Repository: rust-lang/rust
Pull request: tidy-alphabetical: fix line number in error message

<!-- homu-ignore:start -->
<!--
If this PR is related to an unstable feature or an otherwise tracked effort,
please link to the relevant tracking issue here. If you don't know of a related
tracking issue or there are none, feel free to ignore this.

This PR will get automatically assigned to a reviewer. In case you would like
a specific user to review your work, you can assign it to them by using

    r? <reviewer name>
-->
the comment should explain the situation. if it does not please let me know and i'll try to make it better :3

<!-- homu-ignore:end -->


Changed files:
filename: src/tools/tidy/src/alphabetical.rs
patch: @@ -215,7 +215,11 @@ fn check_lines<'a>(path: &Path, content: &'a str, tidy_ctx: &TidyCtx, check: &mu
                 // oh nyooo :(
                 if sorted != section {
                     if !tidy_ctx.is_bless_enabled() {
-                        let base_line_number = content[..offset + start_nl_end].lines().count();
+                        let pre = &content[..offset + start_nl_end];
+                        assert_eq!(pre.chars().rev().next(), Some('\n'));
+                        // start_nl_end spans right after the ␤, so it gets ignored by `lines()`,
+                        // but we do want to count it! so we add 1 to the result.
+                        let base_line_number = pre.lines().count() + 1;
                         let line_offset = sorted
                             .lines()
                             .zip(section.lines())

filename: src/tools/tidy/src/alphabetical/tests.rs
patch: @@ -115,7 +115,7 @@ fn test_rust_bad() {
         def
         // tidy-alphabetical-end
     ";
-    bad(lines, "bad:2: line not in alphabetical order (tip: use --bless to sort this list)");
+    bad(lines, "bad:3: line not in alphabetical order (tip: use --bless to sort this list)");
 }
 
 #[test]
@@ -127,7 +127,7 @@ fn test_toml_bad() {
         def
         # tidy-alphabetical-end
     ";
-    bad(lines, "bad:2: line not in alphabetical order (tip: use --bless to sort this list)");
+    bad(lines, "bad:3: line not in alphabetical order (tip: use --bless to sort this list)");
 }
 
 #[test]
@@ -141,7 +141,7 @@ fn test_features_bad() {
         #![feature(def)]
         tidy-alphabetical-end
     ";
-    bad(lines, "bad:2: line not in alphabetical order (tip: use --bless to sort this list)");
+    bad(lines, "bad:3: line not in alphabetical order (tip: use --bless to sort this list)");
 }
 
 #[test]
@@ -154,7 +154,7 @@ fn test_indent_bad() {
             def
         $ tidy-alphabetical-end
     ";
-    bad(lines, "bad:2: line not in alphabetical order (tip: use --bless to sort this list)");
+    bad(lines, "bad:3: line not in alphabetical order (tip: use --bless to sort this list)");
 }
 
 #[test]
@@ -170,7 +170,7 @@ fn test_split_bad() {
         )
         && tidy-alphabetical-end
     ";
-    bad(lines, "bad:3: line not in alphabetical order (tip: use --bless to sort this list)");
+    bad(lines, "bad:4: line not in alphabetical order (tip: use --bless to sort this list)");
 }
 
 #[test]
@@ -339,23 +339,23 @@ fn test_numeric_bad() {
         item2
         # tidy-alphabetical-end
     ";
-    bad(lines, "bad:2: line not in alphabetical order (tip: use --bless to sort this list)");
+    bad(lines, "bad:3: line not in alphabetical order (tip: use --bless to sort this list)");
 
     let lines = "\
         # tidy-alphabetical-start
         zve64f
         zve64d
         # tidy-alphabetical-end
     ";
-    bad(lines, "bad:1: line not in alphabetical order (tip: use --bless to sort this list)");
+    bad(lines, "bad:2: line not in alphabetical order (tip: use --bless to sort this list)");
 
     let lines = "\
         # tidy-alphabetical-start
         000
         00
         # tidy-alphabetical-end
     ";
-    bad(lines, "bad:1: line not in alphabetical order (tip: use --bless to sort this list)");
+    bad(lines, "bad:2: line not in alphabetical order (tip: use --bless to sort this list)");
 }
 
 #[test]
@@ -394,7 +394,7 @@ fn multiline() {
         );
         tidy-alphabetical-end
     ";
-    bad(lines, "bad:1: line not in alphabetical order (tip: use --bless to sort this list)");
+    bad(lines, "bad:2: line not in alphabetical order (tip: use --bless to sort this list)");
 
     let lines = "\
         tidy-alphabetical-start
@@ -406,7 +406,7 @@ fn multiline() {
          a);
         tidy-alphabetical-end
     ";
-    bad(lines, "bad:1: line not in alphabetical order (tip: use --bless to sort this list)");
+    bad(lines, "bad:2: line not in alphabetical order (tip: use --bless to sort this list)");
 
     let lines = "\
         force_unwind_tables: Option<bool> = (None, parse_opt_bool, [TRACKED],