From 5134a04eaa32b168cf5998a6ec13199356e2e017 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Fri, 10 May 2024 15:34:12 +1000 Subject: [PATCH] Remove `ordinalize`. Some minor (English only) heroics are performed to print error messages like "5th rule of macro `m` is never used". The form "rule #5 of macro `m` is never used" is just as good and much simpler to implement. --- compiler/rustc_resolve/src/diagnostics.rs | 14 ------- .../rustc_resolve/src/diagnostics/tests.rs | 40 ------------------- compiler/rustc_resolve/src/macros.rs | 6 +-- .../unused-macro-rules-compile-error.rs | 6 +-- .../unused-macro-rules-compile-error.stderr | 6 +-- .../ui/lint/unused/unused-macro-rules-decl.rs | 6 +-- .../unused/unused-macro-rules-decl.stderr | 6 +-- tests/ui/lint/unused/unused-macro-rules.rs | 6 +-- .../ui/lint/unused/unused-macro-rules.stderr | 6 +-- 9 files changed, 19 insertions(+), 77 deletions(-) delete mode 100644 compiler/rustc_resolve/src/diagnostics/tests.rs diff --git a/compiler/rustc_resolve/src/diagnostics.rs b/compiler/rustc_resolve/src/diagnostics.rs index a6b0902c09e..b28312fa473 100644 --- a/compiler/rustc_resolve/src/diagnostics.rs +++ b/compiler/rustc_resolve/src/diagnostics.rs @@ -44,9 +44,6 @@ use crate::{LexicalScopeBinding, NameBinding, NameBindingKind, PrivacyError, Vis use crate::{ParentScope, PathResult, ResolutionError, Resolver, Scope, ScopeSet}; use crate::{Segment, UseError}; -#[cfg(test)] -mod tests; - type Res = def::Res; /// A vector of spans and replacements, a message and applicability. @@ -3027,14 +3024,3 @@ fn is_span_suitable_for_use_injection(s: Span) -> bool { // import or other generated ones !s.from_expansion() } - -/// Convert the given number into the corresponding ordinal -pub(crate) fn ordinalize(v: usize) -> String { - let suffix = match ((11..=13).contains(&(v % 100)), v % 10) { - (false, 1) => "st", - (false, 2) => "nd", - (false, 3) => "rd", - _ => "th", - }; - format!("{v}{suffix}") -} diff --git a/compiler/rustc_resolve/src/diagnostics/tests.rs b/compiler/rustc_resolve/src/diagnostics/tests.rs deleted file mode 100644 index 2aa6cc61e46..00000000000 --- a/compiler/rustc_resolve/src/diagnostics/tests.rs +++ /dev/null @@ -1,40 +0,0 @@ -use super::ordinalize; - -#[test] -fn test_ordinalize() { - assert_eq!(ordinalize(1), "1st"); - assert_eq!(ordinalize(2), "2nd"); - assert_eq!(ordinalize(3), "3rd"); - assert_eq!(ordinalize(4), "4th"); - assert_eq!(ordinalize(5), "5th"); - // ... - assert_eq!(ordinalize(10), "10th"); - assert_eq!(ordinalize(11), "11th"); - assert_eq!(ordinalize(12), "12th"); - assert_eq!(ordinalize(13), "13th"); - assert_eq!(ordinalize(14), "14th"); - // ... - assert_eq!(ordinalize(20), "20th"); - assert_eq!(ordinalize(21), "21st"); - assert_eq!(ordinalize(22), "22nd"); - assert_eq!(ordinalize(23), "23rd"); - assert_eq!(ordinalize(24), "24th"); - // ... - assert_eq!(ordinalize(30), "30th"); - assert_eq!(ordinalize(31), "31st"); - assert_eq!(ordinalize(32), "32nd"); - assert_eq!(ordinalize(33), "33rd"); - assert_eq!(ordinalize(34), "34th"); - // ... - assert_eq!(ordinalize(7010), "7010th"); - assert_eq!(ordinalize(7011), "7011th"); - assert_eq!(ordinalize(7012), "7012th"); - assert_eq!(ordinalize(7013), "7013th"); - assert_eq!(ordinalize(7014), "7014th"); - // ... - assert_eq!(ordinalize(7020), "7020th"); - assert_eq!(ordinalize(7021), "7021st"); - assert_eq!(ordinalize(7022), "7022nd"); - assert_eq!(ordinalize(7023), "7023rd"); - assert_eq!(ordinalize(7024), "7024th"); -} diff --git a/compiler/rustc_resolve/src/macros.rs b/compiler/rustc_resolve/src/macros.rs index 35bf3f761df..5161303929d 100644 --- a/compiler/rustc_resolve/src/macros.rs +++ b/compiler/rustc_resolve/src/macros.rs @@ -327,11 +327,7 @@ impl<'a, 'tcx> ResolverExpand for Resolver<'a, 'tcx> { UNUSED_MACRO_RULES, node_id, rule_span, - format!( - "{} rule of macro `{}` is never used", - crate::diagnostics::ordinalize(arm_i + 1), - ident.name - ), + format!("rule #{} of macro `{}` is never used", arm_i + 1, ident.name), ); } } diff --git a/tests/ui/lint/unused/unused-macro-rules-compile-error.rs b/tests/ui/lint/unused/unused-macro-rules-compile-error.rs index 4d51db89bc0..0c8fe65bd5c 100644 --- a/tests/ui/lint/unused/unused-macro-rules-compile-error.rs +++ b/tests/ui/lint/unused/unused-macro-rules-compile-error.rs @@ -9,7 +9,7 @@ macro_rules! num { // Some nested use (two_) => { foo(compile_error!("foo")); }; (three) => { 3 }; - (four) => { 4 }; //~ ERROR: rule of macro + (four) => { 4 }; //~ ERROR: rule #5 of macro } const _NUM: u8 = num!(one) + num!(three); @@ -17,9 +17,9 @@ const _NUM: u8 = num!(one) + num!(three); macro_rules! num2 { (one) => { 1 }; // Only identifier present - (two) => { fn compile_error() {} }; //~ ERROR: rule of macro + (two) => { fn compile_error() {} }; //~ ERROR: rule #2 of macro // Only identifier and bang present - (two_) => { compile_error! }; //~ ERROR: rule of macro + (two_) => { compile_error! }; //~ ERROR: rule #3 of macro (three) => { 3 }; } const _NUM2: u8 = num2!(one) + num2!(three); diff --git a/tests/ui/lint/unused/unused-macro-rules-compile-error.stderr b/tests/ui/lint/unused/unused-macro-rules-compile-error.stderr index 76af8c967db..936428f6a1c 100644 --- a/tests/ui/lint/unused/unused-macro-rules-compile-error.stderr +++ b/tests/ui/lint/unused/unused-macro-rules-compile-error.stderr @@ -1,4 +1,4 @@ -error: 5th rule of macro `num` is never used +error: rule #5 of macro `num` is never used --> $DIR/unused-macro-rules-compile-error.rs:12:5 | LL | (four) => { 4 }; @@ -10,13 +10,13 @@ note: the lint level is defined here LL | #![deny(unused_macro_rules)] | ^^^^^^^^^^^^^^^^^^ -error: 3rd rule of macro `num2` is never used +error: rule #3 of macro `num2` is never used --> $DIR/unused-macro-rules-compile-error.rs:22:5 | LL | (two_) => { compile_error! }; | ^^^^^^ -error: 2nd rule of macro `num2` is never used +error: rule #2 of macro `num2` is never used --> $DIR/unused-macro-rules-compile-error.rs:20:5 | LL | (two) => { fn compile_error() {} }; diff --git a/tests/ui/lint/unused/unused-macro-rules-decl.rs b/tests/ui/lint/unused/unused-macro-rules-decl.rs index 537c84940fd..ca14d58e524 100644 --- a/tests/ui/lint/unused/unused-macro-rules-decl.rs +++ b/tests/ui/lint/unused/unused-macro-rules-decl.rs @@ -6,9 +6,9 @@ // Most simple case macro num { (one) => { 1 }, - (two) => { 2 }, //~ ERROR: 2nd rule of macro + (two) => { 2 }, //~ ERROR: rule #2 of macro (three) => { 3 }, - (four) => { 4 }, //~ ERROR: 4th rule of macro + (four) => { 4 }, //~ ERROR: rule #4 of macro } const _NUM: u8 = num!(one) + num!(three); @@ -28,7 +28,7 @@ macro num_rec { (two) => { num_rec!(one) + num_rec!(one) }, - (three) => { //~ ERROR: 3rd rule of macro + (three) => { //~ ERROR: rule #3 of macro num_rec!(one) + num_rec!(two) }, (four) => { diff --git a/tests/ui/lint/unused/unused-macro-rules-decl.stderr b/tests/ui/lint/unused/unused-macro-rules-decl.stderr index 4d9b22feda2..10ceb3921f3 100644 --- a/tests/ui/lint/unused/unused-macro-rules-decl.stderr +++ b/tests/ui/lint/unused/unused-macro-rules-decl.stderr @@ -1,4 +1,4 @@ -error: 4th rule of macro `num` is never used +error: rule #4 of macro `num` is never used --> $DIR/unused-macro-rules-decl.rs:11:5 | LL | (four) => { 4 }, @@ -10,13 +10,13 @@ note: the lint level is defined here LL | #![deny(unused_macro_rules)] | ^^^^^^^^^^^^^^^^^^ -error: 2nd rule of macro `num` is never used +error: rule #2 of macro `num` is never used --> $DIR/unused-macro-rules-decl.rs:9:5 | LL | (two) => { 2 }, | ^^^^^ -error: 3rd rule of macro `num_rec` is never used +error: rule #3 of macro `num_rec` is never used --> $DIR/unused-macro-rules-decl.rs:31:5 | LL | (three) => { diff --git a/tests/ui/lint/unused/unused-macro-rules.rs b/tests/ui/lint/unused/unused-macro-rules.rs index eeaf4d1b0a9..39a882e8e4d 100644 --- a/tests/ui/lint/unused/unused-macro-rules.rs +++ b/tests/ui/lint/unused/unused-macro-rules.rs @@ -5,9 +5,9 @@ // Most simple case macro_rules! num { (one) => { 1 }; - (two) => { 2 }; //~ ERROR: 2nd rule of macro + (two) => { 2 }; //~ ERROR: rule #2 of macro (three) => { 3 }; - (four) => { 4 }; //~ ERROR: 4th rule of macro + (four) => { 4 }; //~ ERROR: rule #4 of macro } const _NUM: u8 = num!(one) + num!(three); @@ -27,7 +27,7 @@ macro_rules! num_rec { (two) => { num_rec!(one) + num_rec!(one) }; - (three) => { //~ ERROR: 3rd rule of macro + (three) => { //~ ERROR: rule #3 of macro num_rec!(one) + num_rec!(two) }; (four) => { num_rec!(two) + num_rec!(two) }; diff --git a/tests/ui/lint/unused/unused-macro-rules.stderr b/tests/ui/lint/unused/unused-macro-rules.stderr index 2b3098a5128..b9258e77805 100644 --- a/tests/ui/lint/unused/unused-macro-rules.stderr +++ b/tests/ui/lint/unused/unused-macro-rules.stderr @@ -1,4 +1,4 @@ -error: 4th rule of macro `num` is never used +error: rule #4 of macro `num` is never used --> $DIR/unused-macro-rules.rs:10:5 | LL | (four) => { 4 }; @@ -10,13 +10,13 @@ note: the lint level is defined here LL | #![deny(unused_macro_rules)] | ^^^^^^^^^^^^^^^^^^ -error: 2nd rule of macro `num` is never used +error: rule #2 of macro `num` is never used --> $DIR/unused-macro-rules.rs:8:5 | LL | (two) => { 2 }; | ^^^^^ -error: 3rd rule of macro `num_rec` is never used +error: rule #3 of macro `num_rec` is never used --> $DIR/unused-macro-rules.rs:30:5 | LL | (three) => {