Skip to content

Commit f5a1514

Browse files
committed
move check, also lint in macros defined in same crate
1 parent ab67745 commit f5a1514

File tree

3 files changed

+50
-37
lines changed

3 files changed

+50
-37
lines changed

clippy_lints/src/manual_range_pattern.rs

Lines changed: 37 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ use rustc_hir::Expr;
66
use rustc_hir::ExprKind;
77
use rustc_hir::PatKind;
88
use rustc_hir::RangeEnd;
9+
use rustc_lint::LintContext;
910
use rustc_lint::{LateContext, LateLintPass};
11+
use rustc_middle::lint::in_external_macro;
1012
use rustc_session::{declare_lint_pass, declare_tool_lint};
1113

1214
declare_clippy_lint! {
@@ -46,11 +48,14 @@ fn expr_as_u128(expr: &Expr<'_>) -> Option<u128> {
4648

4749
impl LateLintPass<'_> for ManualRangePattern {
4850
fn check_pat(&mut self, cx: &LateContext<'_>, pat: &'_ rustc_hir::Pat<'_>) {
49-
if pat.span.from_expansion() {
51+
if in_external_macro(cx.sess(), pat.span) {
5052
return;
5153
}
5254

53-
if let PatKind::Or(pats) = pat.kind {
55+
// a pattern like 1 | 2 seems fine, lint if there are at least 3 alternatives
56+
if let PatKind::Or(pats) = pat.kind
57+
&& pats.len() >= 3
58+
{
5459
let mut min = u128::MAX;
5560
let mut max = 0;
5661
let mut numbers_found = FxHashSet::default();
@@ -80,41 +85,38 @@ impl LateLintPass<'_> for ManualRangePattern {
8085
}
8186
}
8287

83-
// a pattern like 1 | 2 seems fine, lint if there are at least 3 alternatives
84-
if pats.len() >= 3 {
85-
let contains_whole_range = 'contains: {
86-
let mut num = min;
87-
while num <= max {
88-
if numbers_found.contains(&num) {
89-
num += 1;
90-
}
91-
// Given a list of (potentially overlapping) ranges like:
92-
// 1..=5, 3..=7, 6..=10
93-
// We want to find the range with the highest end that still contains the current number
94-
else if let Some(range) = ranges_found
95-
.iter()
96-
.filter(|range| range.contains(&num))
97-
.max_by_key(|range| range.end())
98-
{
99-
num = range.end() + 1;
100-
} else {
101-
break 'contains false;
102-
}
88+
let contains_whole_range = 'contains: {
89+
let mut num = min;
90+
while num <= max {
91+
if numbers_found.contains(&num) {
92+
num += 1;
93+
}
94+
// Given a list of (potentially overlapping) ranges like:
95+
// 1..=5, 3..=7, 6..=10
96+
// We want to find the range with the highest end that still contains the current number
97+
else if let Some(range) = ranges_found
98+
.iter()
99+
.filter(|range| range.contains(&num))
100+
.max_by_key(|range| range.end())
101+
{
102+
num = range.end() + 1;
103+
} else {
104+
break 'contains false;
103105
}
104-
break 'contains true;
105-
};
106-
107-
if contains_whole_range {
108-
span_lint_and_sugg(
109-
cx,
110-
MANUAL_RANGE_PATTERN,
111-
pat.span,
112-
"this OR pattern can be rewritten using a range",
113-
"try",
114-
format!("{min}..={max}"),
115-
Applicability::MachineApplicable,
116-
);
117106
}
107+
break 'contains true;
108+
};
109+
110+
if contains_whole_range {
111+
span_lint_and_sugg(
112+
cx,
113+
MANUAL_RANGE_PATTERN,
114+
pat.span,
115+
"this OR pattern can be rewritten using a range",
116+
"try",
117+
format!("{min}..={max}"),
118+
Applicability::MachineApplicable,
119+
);
118120
}
119121
}
120122
}

tests/ui/manual_range_pattern.fixed

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ fn main() {
2828

2929
macro_rules! mac {
3030
($e:expr) => {
31-
matches!($e, 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10)
31+
matches!($e, 1..=10)
3232
};
3333
}
3434
mac!(f);

tests/ui/manual_range_pattern.stderr

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,16 @@ error: this OR pattern can be rewritten using a range
3636
LL | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 => true,
3737
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `1..=10`
3838

39-
error: aborting due to 6 previous errors
39+
error: this OR pattern can be rewritten using a range
40+
--> $DIR/manual_range_pattern.rs:31:26
41+
|
42+
LL | matches!($e, 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10)
43+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `1..=10`
44+
...
45+
LL | mac!(f);
46+
| ------- in this macro invocation
47+
|
48+
= note: this error originates in the macro `mac` (in Nightly builds, run with -Z macro-backtrace for more info)
49+
50+
error: aborting due to 7 previous errors
4051

0 commit comments

Comments
 (0)