-
Notifications
You must be signed in to change notification settings - Fork 13.3k
/
Copy pathmod.rs
386 lines (365 loc) · 10.9 KB
/
mod.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
mod builtin_type_shadow;
mod literal_suffix;
mod mixed_case_hex_literals;
mod redundant_at_rest_pattern;
mod redundant_pattern;
mod unneeded_field_pattern;
mod unneeded_wildcard_pattern;
use clippy_utils::diagnostics::span_lint;
use clippy_utils::source::snippet_opt;
use rustc_ast::ast::{Expr, ExprKind, Generics, LitFloatType, LitIntType, LitKind, NodeId, Pat, PatKind};
use rustc_ast::token;
use rustc_ast::visit::FnKind;
use rustc_data_structures::fx::FxHashMap;
use rustc_lint::{EarlyContext, EarlyLintPass, LintContext};
use rustc_session::declare_lint_pass;
use rustc_span::Span;
declare_clippy_lint! {
/// ### What it does
/// Checks for structure field patterns bound to wildcards.
///
/// ### Why restrict this?
/// Using `..` instead is shorter and leaves the focus on
/// the fields that are actually bound.
///
/// ### Example
/// ```no_run
/// # struct Foo {
/// # a: i32,
/// # b: i32,
/// # c: i32,
/// # }
/// let f = Foo { a: 0, b: 0, c: 0 };
///
/// match f {
/// Foo { a: _, b: 0, .. } => {},
/// Foo { a: _, b: _, c: _ } => {},
/// }
/// ```
///
/// Use instead:
/// ```no_run
/// # struct Foo {
/// # a: i32,
/// # b: i32,
/// # c: i32,
/// # }
/// let f = Foo { a: 0, b: 0, c: 0 };
///
/// match f {
/// Foo { b: 0, .. } => {},
/// Foo { .. } => {},
/// }
/// ```
#[clippy::version = "pre 1.29.0"]
pub UNNEEDED_FIELD_PATTERN,
restriction,
"struct fields bound to a wildcard instead of using `..`"
}
declare_clippy_lint! {
/// ### What it does
/// Checks for function arguments having the similar names
/// differing by an underscore.
///
/// ### Why is this bad?
/// It affects code readability.
///
/// ### Example
/// ```no_run
/// fn foo(a: i32, _a: i32) {}
/// ```
///
/// Use instead:
/// ```no_run
/// fn bar(a: i32, _b: i32) {}
/// ```
#[clippy::version = "pre 1.29.0"]
pub DUPLICATE_UNDERSCORE_ARGUMENT,
style,
"function arguments having names which only differ by an underscore"
}
declare_clippy_lint! {
/// ### What it does
/// Warns on hexadecimal literals with mixed-case letter
/// digits.
///
/// ### Why is this bad?
/// It looks confusing.
///
/// ### Example
/// ```no_run
/// # let _ =
/// 0x1a9BAcD
/// # ;
/// ```
///
/// Use instead:
/// ```no_run
/// # let _ =
/// 0x1A9BACD
/// # ;
/// ```
#[clippy::version = "pre 1.29.0"]
pub MIXED_CASE_HEX_LITERALS,
style,
"hex literals whose letter digits are not consistently upper- or lowercased"
}
declare_clippy_lint! {
/// ### What it does
/// Warns if literal suffixes are not separated by an
/// underscore.
/// To enforce unseparated literal suffix style,
/// see the `separated_literal_suffix` lint.
///
/// ### Why restrict this?
/// Suffix style should be consistent.
///
/// ### Example
/// ```no_run
/// # let _ =
/// 123832i32
/// # ;
/// ```
///
/// Use instead:
/// ```no_run
/// # let _ =
/// 123832_i32
/// # ;
/// ```
#[clippy::version = "pre 1.29.0"]
pub UNSEPARATED_LITERAL_SUFFIX,
restriction,
"literals whose suffix is not separated by an underscore"
}
declare_clippy_lint! {
/// ### What it does
/// Warns if literal suffixes are separated by an underscore.
/// To enforce separated literal suffix style,
/// see the `unseparated_literal_suffix` lint.
///
/// ### Why restrict this?
/// Suffix style should be consistent.
///
/// ### Example
/// ```no_run
/// # let _ =
/// 123832_i32
/// # ;
/// ```
///
/// Use instead:
/// ```no_run
/// # let _ =
/// 123832i32
/// # ;
/// ```
#[clippy::version = "1.58.0"]
pub SEPARATED_LITERAL_SUFFIX,
restriction,
"literals whose suffix is separated by an underscore"
}
declare_clippy_lint! {
/// ### What it does
/// Warns if a generic shadows a built-in type.
///
/// ### Why is this bad?
/// This gives surprising type errors.
///
/// ### Example
///
/// ```ignore
/// impl<u32> Foo<u32> {
/// fn impl_func(&self) -> u32 {
/// 42
/// }
/// }
/// ```
#[clippy::version = "pre 1.29.0"]
pub BUILTIN_TYPE_SHADOW,
style,
"shadowing a builtin type"
}
declare_clippy_lint! {
/// ### What it does
/// Checks for patterns in the form `name @ _`.
///
/// ### Why is this bad?
/// It's almost always more readable to just use direct
/// bindings.
///
/// ### Example
/// ```no_run
/// # let v = Some("abc");
/// match v {
/// Some(x) => (),
/// y @ _ => (),
/// }
/// ```
///
/// Use instead:
/// ```no_run
/// # let v = Some("abc");
/// match v {
/// Some(x) => (),
/// y => (),
/// }
/// ```
#[clippy::version = "pre 1.29.0"]
pub REDUNDANT_PATTERN,
style,
"using `name @ _` in a pattern"
}
declare_clippy_lint! {
/// ### What it does
/// Checks for tuple patterns with a wildcard
/// pattern (`_`) is next to a rest pattern (`..`).
///
/// _NOTE_: While `_, ..` means there is at least one element left, `..`
/// means there are 0 or more elements left. This can make a difference
/// when refactoring, but shouldn't result in errors in the refactored code,
/// since the wildcard pattern isn't used anyway.
///
/// ### Why is this bad?
/// The wildcard pattern is unneeded as the rest pattern
/// can match that element as well.
///
/// ### Example
/// ```no_run
/// # struct TupleStruct(u32, u32, u32);
/// # let t = TupleStruct(1, 2, 3);
/// match t {
/// TupleStruct(0, .., _) => (),
/// _ => (),
/// }
/// ```
///
/// Use instead:
/// ```no_run
/// # struct TupleStruct(u32, u32, u32);
/// # let t = TupleStruct(1, 2, 3);
/// match t {
/// TupleStruct(0, ..) => (),
/// _ => (),
/// }
/// ```
#[clippy::version = "1.40.0"]
pub UNNEEDED_WILDCARD_PATTERN,
complexity,
"tuple patterns with a wildcard pattern (`_`) is next to a rest pattern (`..`)"
}
declare_clippy_lint! {
/// ### What it does
/// Checks for `[all @ ..]` patterns.
///
/// ### Why is this bad?
/// In all cases, `all` works fine and can often make code simpler, as you possibly won't need
/// to convert from say a `Vec` to a slice by dereferencing.
///
/// ### Example
/// ```rust,ignore
/// if let [all @ ..] = &*v {
/// // NOTE: Type is a slice here
/// println!("all elements: {all:#?}");
/// }
/// ```
/// Use instead:
/// ```rust,ignore
/// if let all = v {
/// // NOTE: Type is a `Vec` here
/// println!("all elements: {all:#?}");
/// }
/// // or
/// println!("all elements: {v:#?}");
/// ```
#[clippy::version = "1.72.0"]
pub REDUNDANT_AT_REST_PATTERN,
complexity,
"checks for `[all @ ..]` where `all` would suffice"
}
declare_lint_pass!(MiscEarlyLints => [
UNNEEDED_FIELD_PATTERN,
DUPLICATE_UNDERSCORE_ARGUMENT,
MIXED_CASE_HEX_LITERALS,
UNSEPARATED_LITERAL_SUFFIX,
SEPARATED_LITERAL_SUFFIX,
BUILTIN_TYPE_SHADOW,
REDUNDANT_PATTERN,
UNNEEDED_WILDCARD_PATTERN,
REDUNDANT_AT_REST_PATTERN,
]);
impl EarlyLintPass for MiscEarlyLints {
fn check_generics(&mut self, cx: &EarlyContext<'_>, generics: &Generics) {
for param in &generics.params {
builtin_type_shadow::check(cx, param);
}
}
fn check_pat(&mut self, cx: &EarlyContext<'_>, pat: &Pat) {
if pat.span.in_external_macro(cx.sess().source_map()) {
return;
}
unneeded_field_pattern::check(cx, pat);
redundant_pattern::check(cx, pat);
redundant_at_rest_pattern::check(cx, pat);
unneeded_wildcard_pattern::check(cx, pat);
}
fn check_fn(&mut self, cx: &EarlyContext<'_>, fn_kind: FnKind<'_>, _: Span, _: NodeId) {
let mut registered_names: FxHashMap<String, Span> = FxHashMap::default();
for arg in &fn_kind.decl().inputs {
if let PatKind::Ident(_, ident, None) = arg.pat.kind {
let arg_name = ident.to_string();
if let Some(arg_name) = arg_name.strip_prefix('_') {
if let Some(correspondence) = registered_names.get(arg_name) {
span_lint(
cx,
DUPLICATE_UNDERSCORE_ARGUMENT,
*correspondence,
format!(
"`{arg_name}` already exists, having another argument having almost the same \
name makes code comprehension and documentation more difficult"
),
);
}
} else {
registered_names.insert(arg_name, arg.pat.span);
}
}
}
}
fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) {
if expr.span.in_external_macro(cx.sess().source_map()) {
return;
}
if let ExprKind::Lit(lit) = expr.kind {
MiscEarlyLints::check_lit(cx, lit, expr.span);
}
}
}
impl MiscEarlyLints {
fn check_lit(cx: &EarlyContext<'_>, lit: token::Lit, span: Span) {
// We test if first character in snippet is a number, because the snippet could be an expansion
// from a built-in macro like `line!()` or a proc-macro like `#[wasm_bindgen]`.
// Note that this check also covers special case that `line!()` is eagerly expanded by compiler.
// See <https://github.com/rust-lang/rust-clippy/issues/4507> for a regression.
// FIXME: Find a better way to detect those cases.
let lit_snip = match snippet_opt(cx, span) {
Some(snip) if snip.chars().next().is_some_and(|c| c.is_ascii_digit()) => snip,
_ => return,
};
let lit_kind = LitKind::from_token_lit(lit);
if let Ok(LitKind::Int(_value, lit_int_type)) = lit_kind {
let suffix = match lit_int_type {
LitIntType::Signed(ty) => ty.name_str(),
LitIntType::Unsigned(ty) => ty.name_str(),
LitIntType::Unsuffixed => "",
};
literal_suffix::check(cx, span, &lit_snip, suffix, "integer");
if lit_snip.starts_with("0x") {
mixed_case_hex_literals::check(cx, span, suffix, &lit_snip);
}
} else if let Ok(LitKind::Float(_, LitFloatType::Suffixed(float_ty))) = lit_kind {
let suffix = float_ty.name_str();
literal_suffix::check(cx, span, &lit_snip, suffix, "float");
}
}
}