Skip to content

Commit b18a3c5

Browse files
authored
Merge pull request #3007 from mikerite/issue3000
Fix #3000
2 parents f6c4e30 + ffce3c7 commit b18a3c5

File tree

4 files changed

+21
-55
lines changed

4 files changed

+21
-55
lines changed

clippy_lints/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ mod reexport {
178178

179179
pub fn register_pre_expansion_lints(session: &rustc::session::Session, store: &mut rustc::lint::LintStore) {
180180
store.register_pre_expansion_pass(Some(session), box write::Pass);
181+
store.register_pre_expansion_pass(Some(session), box redundant_field_names::RedundantFieldNames);
181182
}
182183

183184
#[cfg_attr(rustfmt, rustfmt_skip)]
@@ -390,7 +391,6 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>) {
390391
reg.register_late_lint_pass(box double_comparison::DoubleComparisonPass);
391392
reg.register_late_lint_pass(box question_mark::QuestionMarkPass);
392393
reg.register_late_lint_pass(box suspicious_trait_impl::SuspiciousImpl);
393-
reg.register_late_lint_pass(box redundant_field_names::RedundantFieldNames);
394394
reg.register_early_lint_pass(box multiple_crate_versions::Pass);
395395
reg.register_late_lint_pass(box map_unit_fn::Pass);
396396
reg.register_late_lint_pass(box infallible_destructuring_match::Pass);

clippy_lints/src/redundant_field_names.rs

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use rustc::lint::*;
22
use rustc::{declare_lint, lint_array};
3-
use rustc::hir::*;
4-
use crate::utils::{in_macro, match_var, span_lint_and_sugg};
3+
use syntax::ast::*;
4+
use crate::utils::{span_lint_and_sugg};
55

66
/// **What it does:** Checks for fields in struct literals where shorthands
77
/// could be used.
@@ -35,28 +35,24 @@ impl LintPass for RedundantFieldNames {
3535
}
3636
}
3737

38-
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for RedundantFieldNames {
39-
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
40-
// Ignore all macros including range expressions.
41-
// They can have redundant field names when expanded.
42-
// e.g. range expression `start..end` is desugared to `Range { start: start, end: end }`
43-
if in_macro(expr.span) {
44-
return;
45-
}
46-
38+
impl EarlyLintPass for RedundantFieldNames {
39+
fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) {
4740
if let ExprKind::Struct(_, ref fields, _) = expr.node {
4841
for field in fields {
49-
let name = field.ident.name;
50-
51-
if match_var(&field.expr, name) && !field.is_shorthand {
52-
span_lint_and_sugg (
53-
cx,
54-
REDUNDANT_FIELD_NAMES,
55-
field.span,
56-
"redundant field names in struct initialization",
57-
"replace it with",
58-
name.to_string()
59-
);
42+
if field.is_shorthand {
43+
continue;
44+
}
45+
if let ExprKind::Path(None, path) = &field.expr.node {
46+
if path.segments.len() == 1 && path.segments[0].ident == field.ident {
47+
span_lint_and_sugg (
48+
cx,
49+
REDUNDANT_FIELD_NAMES,
50+
field.span,
51+
"redundant field names in struct initialization",
52+
"replace it with",
53+
field.ident.to_string()
54+
);
55+
}
6056
}
6157
}
6258
}

tests/ui/redundant_field_names.stderr

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -12,36 +12,6 @@ error: redundant field names in struct initialization
1212
35 | age: age,
1313
| ^^^^^^^^ help: replace it with: `age`
1414

15-
error: redundant field names in struct initialization
16-
--> $DIR/redundant_field_names.rs:45:13
17-
|
18-
45 | let _ = start..;
19-
| ^^^^^ help: replace it with: `start`
20-
21-
error: redundant field names in struct initialization
22-
--> $DIR/redundant_field_names.rs:46:15
23-
|
24-
46 | let _ = ..end;
25-
| ^^^ help: replace it with: `end`
26-
27-
error: redundant field names in struct initialization
28-
--> $DIR/redundant_field_names.rs:47:13
29-
|
30-
47 | let _ = start..end;
31-
| ^^^^^ help: replace it with: `start`
32-
33-
error: redundant field names in struct initialization
34-
--> $DIR/redundant_field_names.rs:47:20
35-
|
36-
47 | let _ = start..end;
37-
| ^^^ help: replace it with: `end`
38-
39-
error: redundant field names in struct initialization
40-
--> $DIR/redundant_field_names.rs:49:16
41-
|
42-
49 | let _ = ..=end;
43-
| ^^^ help: replace it with: `end`
44-
4515
error: redundant field names in struct initialization
4616
--> $DIR/redundant_field_names.rs:53:25
4717
|
@@ -72,5 +42,5 @@ error: redundant field names in struct initialization
7242
57 | let _ = RangeToInclusive { end: end };
7343
| ^^^^^^^^ help: replace it with: `end`
7444

75-
error: aborting due to 12 previous errors
45+
error: aborting due to 7 previous errors
7646

tests/ui/trivially_copy_pass_by_ref.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![allow(many_single_char_names, blacklisted_name)]
1+
#![allow(many_single_char_names, blacklisted_name, redundant_field_names)]
22

33
#[derive(Copy, Clone)]
44
struct Foo(u32);

0 commit comments

Comments
 (0)