@@ -6,7 +6,9 @@ use rustc_hir::Expr;
6
6
use rustc_hir:: ExprKind ;
7
7
use rustc_hir:: PatKind ;
8
8
use rustc_hir:: RangeEnd ;
9
+ use rustc_lint:: LintContext ;
9
10
use rustc_lint:: { LateContext , LateLintPass } ;
11
+ use rustc_middle:: lint:: in_external_macro;
10
12
use rustc_session:: { declare_lint_pass, declare_tool_lint} ;
11
13
12
14
declare_clippy_lint ! {
@@ -46,11 +48,14 @@ fn expr_as_u128(expr: &Expr<'_>) -> Option<u128> {
46
48
47
49
impl LateLintPass < ' _ > for ManualRangePattern {
48
50
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 ) {
50
52
return ;
51
53
}
52
54
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
+ {
54
59
let mut min = u128:: MAX ;
55
60
let mut max = 0 ;
56
61
let mut numbers_found = FxHashSet :: default ( ) ;
@@ -80,41 +85,38 @@ impl LateLintPass<'_> for ManualRangePattern {
80
85
}
81
86
}
82
87
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 ;
103
105
}
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
- ) ;
117
106
}
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
+ ) ;
118
120
}
119
121
}
120
122
}
0 commit comments