From ffce3c77e4e1dd997b1713e0c2bf151e612fc313 Mon Sep 17 00:00:00 2001 From: Michael Wright Date: Mon, 6 Aug 2018 08:20:50 +0200 Subject: [PATCH] Fix #3000 --- clippy_lints/src/lib.rs | 2 +- clippy_lints/src/redundant_field_names.rs | 40 ++++++++++------------- tests/ui/redundant_field_names.stderr | 32 +----------------- tests/ui/trivially_copy_pass_by_ref.rs | 2 +- 4 files changed, 21 insertions(+), 55 deletions(-) diff --git a/clippy_lints/src/lib.rs b/clippy_lints/src/lib.rs index 31a28c2a0..5b6b362fd 100644 --- a/clippy_lints/src/lib.rs +++ b/clippy_lints/src/lib.rs @@ -178,6 +178,7 @@ mod reexport { pub fn register_pre_expansion_lints(session: &rustc::session::Session, store: &mut rustc::lint::LintStore) { store.register_pre_expansion_pass(Some(session), box write::Pass); + store.register_pre_expansion_pass(Some(session), box redundant_field_names::RedundantFieldNames); } #[cfg_attr(rustfmt, rustfmt_skip)] @@ -390,7 +391,6 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>) { reg.register_late_lint_pass(box double_comparison::DoubleComparisonPass); reg.register_late_lint_pass(box question_mark::QuestionMarkPass); reg.register_late_lint_pass(box suspicious_trait_impl::SuspiciousImpl); - reg.register_late_lint_pass(box redundant_field_names::RedundantFieldNames); reg.register_early_lint_pass(box multiple_crate_versions::Pass); reg.register_late_lint_pass(box map_unit_fn::Pass); reg.register_late_lint_pass(box infallible_destructuring_match::Pass); diff --git a/clippy_lints/src/redundant_field_names.rs b/clippy_lints/src/redundant_field_names.rs index 27f890ccd..c5cf35eda 100644 --- a/clippy_lints/src/redundant_field_names.rs +++ b/clippy_lints/src/redundant_field_names.rs @@ -1,7 +1,7 @@ use rustc::lint::*; use rustc::{declare_lint, lint_array}; -use rustc::hir::*; -use crate::utils::{in_macro, match_var, span_lint_and_sugg}; +use syntax::ast::*; +use crate::utils::{span_lint_and_sugg}; /// **What it does:** Checks for fields in struct literals where shorthands /// could be used. @@ -35,28 +35,24 @@ impl LintPass for RedundantFieldNames { } } -impl<'a, 'tcx> LateLintPass<'a, 'tcx> for RedundantFieldNames { - fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) { - // Ignore all macros including range expressions. - // They can have redundant field names when expanded. - // e.g. range expression `start..end` is desugared to `Range { start: start, end: end }` - if in_macro(expr.span) { - return; - } - +impl EarlyLintPass for RedundantFieldNames { + fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) { if let ExprKind::Struct(_, ref fields, _) = expr.node { for field in fields { - let name = field.ident.name; - - if match_var(&field.expr, name) && !field.is_shorthand { - span_lint_and_sugg ( - cx, - REDUNDANT_FIELD_NAMES, - field.span, - "redundant field names in struct initialization", - "replace it with", - name.to_string() - ); + if field.is_shorthand { + continue; + } + if let ExprKind::Path(None, path) = &field.expr.node { + if path.segments.len() == 1 && path.segments[0].ident == field.ident { + span_lint_and_sugg ( + cx, + REDUNDANT_FIELD_NAMES, + field.span, + "redundant field names in struct initialization", + "replace it with", + field.ident.to_string() + ); + } } } } diff --git a/tests/ui/redundant_field_names.stderr b/tests/ui/redundant_field_names.stderr index 4f706d1fe..d757f1871 100644 --- a/tests/ui/redundant_field_names.stderr +++ b/tests/ui/redundant_field_names.stderr @@ -12,36 +12,6 @@ error: redundant field names in struct initialization 35 | age: age, | ^^^^^^^^ help: replace it with: `age` -error: redundant field names in struct initialization - --> $DIR/redundant_field_names.rs:45:13 - | -45 | let _ = start..; - | ^^^^^ help: replace it with: `start` - -error: redundant field names in struct initialization - --> $DIR/redundant_field_names.rs:46:15 - | -46 | let _ = ..end; - | ^^^ help: replace it with: `end` - -error: redundant field names in struct initialization - --> $DIR/redundant_field_names.rs:47:13 - | -47 | let _ = start..end; - | ^^^^^ help: replace it with: `start` - -error: redundant field names in struct initialization - --> $DIR/redundant_field_names.rs:47:20 - | -47 | let _ = start..end; - | ^^^ help: replace it with: `end` - -error: redundant field names in struct initialization - --> $DIR/redundant_field_names.rs:49:16 - | -49 | let _ = ..=end; - | ^^^ help: replace it with: `end` - error: redundant field names in struct initialization --> $DIR/redundant_field_names.rs:53:25 | @@ -72,5 +42,5 @@ error: redundant field names in struct initialization 57 | let _ = RangeToInclusive { end: end }; | ^^^^^^^^ help: replace it with: `end` -error: aborting due to 12 previous errors +error: aborting due to 7 previous errors diff --git a/tests/ui/trivially_copy_pass_by_ref.rs b/tests/ui/trivially_copy_pass_by_ref.rs index 9b905e8d6..a1a1de1e4 100644 --- a/tests/ui/trivially_copy_pass_by_ref.rs +++ b/tests/ui/trivially_copy_pass_by_ref.rs @@ -1,4 +1,4 @@ -#![allow(many_single_char_names, blacklisted_name)] +#![allow(many_single_char_names, blacklisted_name, redundant_field_names)] #[derive(Copy, Clone)] struct Foo(u32);