From 178f5029705b7713829b3435c0d933cfb6c303ed Mon Sep 17 00:00:00 2001 From: "S. van Dijk" Date: Sun, 11 Sep 2022 20:15:35 +0200 Subject: [PATCH 1/3] Stop lint from checking code expanded from macros. --- clippy_lints/src/almost_complete_letter_range.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/clippy_lints/src/almost_complete_letter_range.rs b/clippy_lints/src/almost_complete_letter_range.rs index 59a7c535400..2036dca0032 100644 --- a/clippy_lints/src/almost_complete_letter_range.rs +++ b/clippy_lints/src/almost_complete_letter_range.rs @@ -79,6 +79,7 @@ fn check_range(cx: &EarlyContext<'_>, span: Span, start: &Expr, end: &Expr, sugg (LitKind::Byte(b'a') | LitKind::Char('a'), LitKind::Byte(b'z') | LitKind::Char('z')) | (LitKind::Byte(b'A') | LitKind::Char('A'), LitKind::Byte(b'Z') | LitKind::Char('Z')) ) + && !span.from_expansion() { span_lint_and_then( cx, From 4a6f7abeebfa5c462458b788ccd122841408f062 Mon Sep 17 00:00:00 2001 From: "S. van Dijk" Date: Mon, 12 Sep 2022 10:44:44 +0200 Subject: [PATCH 2/3] Add test. --- tests/ui/almost_complete_letter_range.fixed | 7 ++++++ tests/ui/almost_complete_letter_range.rs | 7 ++++++ tests/ui/almost_complete_letter_range.stderr | 24 ++++++++++---------- 3 files changed, 26 insertions(+), 12 deletions(-) diff --git a/tests/ui/almost_complete_letter_range.fixed b/tests/ui/almost_complete_letter_range.fixed index e69b40f35f4..0e3f644ca45 100644 --- a/tests/ui/almost_complete_letter_range.fixed +++ b/tests/ui/almost_complete_letter_range.fixed @@ -14,6 +14,12 @@ macro_rules! a { }; } +macro_rules! b { + () => { + let _ = 'a'..'z'; + }; +} + fn main() { #[rustfmt::skip] { @@ -31,6 +37,7 @@ fn main() { let _ = b'B'..b'Z'; let _ = a!()..='z'; + b!(); let _ = match 0u8 { b'a'..=b'z' if true => 1, diff --git a/tests/ui/almost_complete_letter_range.rs b/tests/ui/almost_complete_letter_range.rs index f2240981d45..60402bf39ed 100644 --- a/tests/ui/almost_complete_letter_range.rs +++ b/tests/ui/almost_complete_letter_range.rs @@ -14,6 +14,12 @@ macro_rules! a { }; } +macro_rules! b { + () => { + let _ = 'a'..'z'; + }; +} + fn main() { #[rustfmt::skip] { @@ -31,6 +37,7 @@ fn main() { let _ = b'B'..b'Z'; let _ = a!()..'z'; + b!(); let _ = match 0u8 { b'a'..b'z' if true => 1, diff --git a/tests/ui/almost_complete_letter_range.stderr b/tests/ui/almost_complete_letter_range.stderr index 5b5dc40ee54..71acae690d3 100644 --- a/tests/ui/almost_complete_letter_range.stderr +++ b/tests/ui/almost_complete_letter_range.stderr @@ -1,5 +1,5 @@ error: almost complete ascii letter range - --> $DIR/almost_complete_letter_range.rs:20:17 + --> $DIR/almost_complete_letter_range.rs:26:17 | LL | let _ = ('a') ..'z'; | ^^^^^^--^^^ @@ -9,7 +9,7 @@ LL | let _ = ('a') ..'z'; = note: `-D clippy::almost-complete-letter-range` implied by `-D warnings` error: almost complete ascii letter range - --> $DIR/almost_complete_letter_range.rs:21:17 + --> $DIR/almost_complete_letter_range.rs:27:17 | LL | let _ = 'A' .. ('Z'); | ^^^^--^^^^^^ @@ -17,7 +17,7 @@ LL | let _ = 'A' .. ('Z'); | help: use an inclusive range: `..=` error: almost complete ascii letter range - --> $DIR/almost_complete_letter_range.rs:27:13 + --> $DIR/almost_complete_letter_range.rs:33:13 | LL | let _ = (b'a')..(b'z'); | ^^^^^^--^^^^^^ @@ -25,7 +25,7 @@ LL | let _ = (b'a')..(b'z'); | help: use an inclusive range: `..=` error: almost complete ascii letter range - --> $DIR/almost_complete_letter_range.rs:28:13 + --> $DIR/almost_complete_letter_range.rs:34:13 | LL | let _ = b'A'..b'Z'; | ^^^^--^^^^ @@ -33,7 +33,7 @@ LL | let _ = b'A'..b'Z'; | help: use an inclusive range: `..=` error: almost complete ascii letter range - --> $DIR/almost_complete_letter_range.rs:33:13 + --> $DIR/almost_complete_letter_range.rs:39:13 | LL | let _ = a!()..'z'; | ^^^^--^^^ @@ -41,7 +41,7 @@ LL | let _ = a!()..'z'; | help: use an inclusive range: `..=` error: almost complete ascii letter range - --> $DIR/almost_complete_letter_range.rs:36:9 + --> $DIR/almost_complete_letter_range.rs:43:9 | LL | b'a'..b'z' if true => 1, | ^^^^--^^^^ @@ -49,7 +49,7 @@ LL | b'a'..b'z' if true => 1, | help: use an inclusive range: `..=` error: almost complete ascii letter range - --> $DIR/almost_complete_letter_range.rs:37:9 + --> $DIR/almost_complete_letter_range.rs:44:9 | LL | b'A'..b'Z' if true => 2, | ^^^^--^^^^ @@ -57,7 +57,7 @@ LL | b'A'..b'Z' if true => 2, | help: use an inclusive range: `..=` error: almost complete ascii letter range - --> $DIR/almost_complete_letter_range.rs:44:9 + --> $DIR/almost_complete_letter_range.rs:51:9 | LL | 'a'..'z' if true => 1, | ^^^--^^^ @@ -65,7 +65,7 @@ LL | 'a'..'z' if true => 1, | help: use an inclusive range: `..=` error: almost complete ascii letter range - --> $DIR/almost_complete_letter_range.rs:45:9 + --> $DIR/almost_complete_letter_range.rs:52:9 | LL | 'A'..'Z' if true => 2, | ^^^--^^^ @@ -73,7 +73,7 @@ LL | 'A'..'Z' if true => 2, | help: use an inclusive range: `..=` error: almost complete ascii letter range - --> $DIR/almost_complete_letter_range.rs:55:9 + --> $DIR/almost_complete_letter_range.rs:62:9 | LL | 'a'..'z' => 1, | ^^^--^^^ @@ -81,7 +81,7 @@ LL | 'a'..'z' => 1, | help: use an inclusive range: `...` error: almost complete ascii letter range - --> $DIR/almost_complete_letter_range.rs:62:13 + --> $DIR/almost_complete_letter_range.rs:69:13 | LL | let _ = 'a'..'z'; | ^^^--^^^ @@ -89,7 +89,7 @@ LL | let _ = 'a'..'z'; | help: use an inclusive range: `..=` error: almost complete ascii letter range - --> $DIR/almost_complete_letter_range.rs:64:9 + --> $DIR/almost_complete_letter_range.rs:71:9 | LL | 'a'..'z' => 1, | ^^^--^^^ From 69c7d2cca29f6810744714bbd2c15cbe166c0741 Mon Sep 17 00:00:00 2001 From: "S. van Dijk" Date: Tue, 13 Sep 2022 09:51:32 +0200 Subject: [PATCH 3/3] Only ignore external macros. --- .../src/almost_complete_letter_range.rs | 3 +- tests/ui/almost_complete_letter_range.fixed | 10 ++++- tests/ui/almost_complete_letter_range.rs | 8 +++- tests/ui/almost_complete_letter_range.stderr | 39 ++++++++++++------- tests/ui/auxiliary/macro_rules.rs | 7 ++++ 5 files changed, 50 insertions(+), 17 deletions(-) diff --git a/clippy_lints/src/almost_complete_letter_range.rs b/clippy_lints/src/almost_complete_letter_range.rs index 2036dca0032..073e4af1318 100644 --- a/clippy_lints/src/almost_complete_letter_range.rs +++ b/clippy_lints/src/almost_complete_letter_range.rs @@ -4,6 +4,7 @@ use clippy_utils::{meets_msrv, msrvs}; use rustc_ast::ast::{Expr, ExprKind, LitKind, Pat, PatKind, RangeEnd, RangeLimits}; use rustc_errors::Applicability; use rustc_lint::{EarlyContext, EarlyLintPass, LintContext}; +use rustc_middle::lint::in_external_macro; use rustc_semver::RustcVersion; use rustc_session::{declare_tool_lint, impl_lint_pass}; use rustc_span::Span; @@ -79,7 +80,7 @@ fn check_range(cx: &EarlyContext<'_>, span: Span, start: &Expr, end: &Expr, sugg (LitKind::Byte(b'a') | LitKind::Char('a'), LitKind::Byte(b'z') | LitKind::Char('z')) | (LitKind::Byte(b'A') | LitKind::Char('A'), LitKind::Byte(b'Z') | LitKind::Char('Z')) ) - && !span.from_expansion() + && !in_external_macro(cx.sess(), span) { span_lint_and_then( cx, diff --git a/tests/ui/almost_complete_letter_range.fixed b/tests/ui/almost_complete_letter_range.fixed index 0e3f644ca45..079b7c000dc 100644 --- a/tests/ui/almost_complete_letter_range.fixed +++ b/tests/ui/almost_complete_letter_range.fixed @@ -1,5 +1,6 @@ // run-rustfix // edition:2018 +// aux-build:macro_rules.rs #![feature(custom_inner_attributes)] #![feature(exclusive_range_pattern)] @@ -8,6 +9,9 @@ #![allow(ellipsis_inclusive_range_patterns)] #![allow(clippy::needless_parens_on_range_literals)] +#[macro_use] +extern crate macro_rules; + macro_rules! a { () => { 'a' @@ -16,7 +20,7 @@ macro_rules! a { macro_rules! b { () => { - let _ = 'a'..'z'; + let _ = 'a'..='z'; }; } @@ -37,7 +41,6 @@ fn main() { let _ = b'B'..b'Z'; let _ = a!()..='z'; - b!(); let _ = match 0u8 { b'a'..=b'z' if true => 1, @@ -54,6 +57,9 @@ fn main() { 'B'..'Z' => 4, _ => 5, }; + + almost_complete_letter_range!(); + b!(); } fn _under_msrv() { diff --git a/tests/ui/almost_complete_letter_range.rs b/tests/ui/almost_complete_letter_range.rs index 60402bf39ed..a66900a976e 100644 --- a/tests/ui/almost_complete_letter_range.rs +++ b/tests/ui/almost_complete_letter_range.rs @@ -1,5 +1,6 @@ // run-rustfix // edition:2018 +// aux-build:macro_rules.rs #![feature(custom_inner_attributes)] #![feature(exclusive_range_pattern)] @@ -8,6 +9,9 @@ #![allow(ellipsis_inclusive_range_patterns)] #![allow(clippy::needless_parens_on_range_literals)] +#[macro_use] +extern crate macro_rules; + macro_rules! a { () => { 'a' @@ -37,7 +41,6 @@ fn main() { let _ = b'B'..b'Z'; let _ = a!()..'z'; - b!(); let _ = match 0u8 { b'a'..b'z' if true => 1, @@ -54,6 +57,9 @@ fn main() { 'B'..'Z' => 4, _ => 5, }; + + almost_complete_letter_range!(); + b!(); } fn _under_msrv() { diff --git a/tests/ui/almost_complete_letter_range.stderr b/tests/ui/almost_complete_letter_range.stderr index 71acae690d3..3de44c72c1b 100644 --- a/tests/ui/almost_complete_letter_range.stderr +++ b/tests/ui/almost_complete_letter_range.stderr @@ -1,5 +1,5 @@ error: almost complete ascii letter range - --> $DIR/almost_complete_letter_range.rs:26:17 + --> $DIR/almost_complete_letter_range.rs:30:17 | LL | let _ = ('a') ..'z'; | ^^^^^^--^^^ @@ -9,7 +9,7 @@ LL | let _ = ('a') ..'z'; = note: `-D clippy::almost-complete-letter-range` implied by `-D warnings` error: almost complete ascii letter range - --> $DIR/almost_complete_letter_range.rs:27:17 + --> $DIR/almost_complete_letter_range.rs:31:17 | LL | let _ = 'A' .. ('Z'); | ^^^^--^^^^^^ @@ -17,7 +17,7 @@ LL | let _ = 'A' .. ('Z'); | help: use an inclusive range: `..=` error: almost complete ascii letter range - --> $DIR/almost_complete_letter_range.rs:33:13 + --> $DIR/almost_complete_letter_range.rs:37:13 | LL | let _ = (b'a')..(b'z'); | ^^^^^^--^^^^^^ @@ -25,7 +25,7 @@ LL | let _ = (b'a')..(b'z'); | help: use an inclusive range: `..=` error: almost complete ascii letter range - --> $DIR/almost_complete_letter_range.rs:34:13 + --> $DIR/almost_complete_letter_range.rs:38:13 | LL | let _ = b'A'..b'Z'; | ^^^^--^^^^ @@ -33,7 +33,7 @@ LL | let _ = b'A'..b'Z'; | help: use an inclusive range: `..=` error: almost complete ascii letter range - --> $DIR/almost_complete_letter_range.rs:39:13 + --> $DIR/almost_complete_letter_range.rs:43:13 | LL | let _ = a!()..'z'; | ^^^^--^^^ @@ -41,7 +41,7 @@ LL | let _ = a!()..'z'; | help: use an inclusive range: `..=` error: almost complete ascii letter range - --> $DIR/almost_complete_letter_range.rs:43:9 + --> $DIR/almost_complete_letter_range.rs:46:9 | LL | b'a'..b'z' if true => 1, | ^^^^--^^^^ @@ -49,7 +49,7 @@ LL | b'a'..b'z' if true => 1, | help: use an inclusive range: `..=` error: almost complete ascii letter range - --> $DIR/almost_complete_letter_range.rs:44:9 + --> $DIR/almost_complete_letter_range.rs:47:9 | LL | b'A'..b'Z' if true => 2, | ^^^^--^^^^ @@ -57,7 +57,7 @@ LL | b'A'..b'Z' if true => 2, | help: use an inclusive range: `..=` error: almost complete ascii letter range - --> $DIR/almost_complete_letter_range.rs:51:9 + --> $DIR/almost_complete_letter_range.rs:54:9 | LL | 'a'..'z' if true => 1, | ^^^--^^^ @@ -65,7 +65,7 @@ LL | 'a'..'z' if true => 1, | help: use an inclusive range: `..=` error: almost complete ascii letter range - --> $DIR/almost_complete_letter_range.rs:52:9 + --> $DIR/almost_complete_letter_range.rs:55:9 | LL | 'A'..'Z' if true => 2, | ^^^--^^^ @@ -73,7 +73,20 @@ LL | 'A'..'Z' if true => 2, | help: use an inclusive range: `..=` error: almost complete ascii letter range - --> $DIR/almost_complete_letter_range.rs:62:9 + --> $DIR/almost_complete_letter_range.rs:23:17 + | +LL | let _ = 'a'..'z'; + | ^^^--^^^ + | | + | help: use an inclusive range: `..=` +... +LL | b!(); + | ---- in this macro invocation + | + = note: this error originates in the macro `b` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: almost complete ascii letter range + --> $DIR/almost_complete_letter_range.rs:68:9 | LL | 'a'..'z' => 1, | ^^^--^^^ @@ -81,7 +94,7 @@ LL | 'a'..'z' => 1, | help: use an inclusive range: `...` error: almost complete ascii letter range - --> $DIR/almost_complete_letter_range.rs:69:13 + --> $DIR/almost_complete_letter_range.rs:75:13 | LL | let _ = 'a'..'z'; | ^^^--^^^ @@ -89,12 +102,12 @@ LL | let _ = 'a'..'z'; | help: use an inclusive range: `..=` error: almost complete ascii letter range - --> $DIR/almost_complete_letter_range.rs:71:9 + --> $DIR/almost_complete_letter_range.rs:77:9 | LL | 'a'..'z' => 1, | ^^^--^^^ | | | help: use an inclusive range: `..=` -error: aborting due to 12 previous errors +error: aborting due to 13 previous errors diff --git a/tests/ui/auxiliary/macro_rules.rs b/tests/ui/auxiliary/macro_rules.rs index 83a0af6b87a..ef3ca9aea38 100644 --- a/tests/ui/auxiliary/macro_rules.rs +++ b/tests/ui/auxiliary/macro_rules.rs @@ -140,3 +140,10 @@ macro_rules! manual_rem_euclid { macro_rules! equatable_if_let { ($a:ident) => {{ if let 2 = $a {} }}; } + +#[macro_export] +macro_rules! almost_complete_letter_range { + () => { + let _ = 'a'..'z'; + }; +}