Skip to content

Commit

Permalink
Implement redundant field names lint #2244
Browse files Browse the repository at this point in the history
  • Loading branch information
to-mas-kral committed Feb 10, 2018
1 parent 6b3487a commit 85642dd
Show file tree
Hide file tree
Showing 6 changed files with 210 additions and 94 deletions.
3 changes: 3 additions & 0 deletions clippy_lints/src/lib.rs
Expand Up @@ -152,6 +152,7 @@ pub mod ptr;
pub mod question_mark;
pub mod ranges;
pub mod reference;
pub mod redundant_field_names;
pub mod regex;
pub mod replace_consts;
pub mod returns;
Expand Down Expand Up @@ -373,6 +374,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry) {
reg.register_late_lint_pass(box types::UnitArg);
reg.register_late_lint_pass(box double_comparison::DoubleComparisonPass);
reg.register_late_lint_pass(box question_mark::QuestionMarkPass);
reg.register_late_lint_pass(box redundant_field_names::RedundantFieldNames);

reg.register_lint_group("clippy_restrictions", vec![
arithmetic::FLOAT_ARITHMETIC,
Expand Down Expand Up @@ -591,6 +593,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry) {
ranges::ITERATOR_STEP_BY_ZERO,
ranges::RANGE_MINUS_ONE,
ranges::RANGE_ZIP_WITH_LEN,
redundant_field_names::REDUNDANT_FIELD_NAMES,
reference::DEREF_ADDROF,
regex::INVALID_REGEX,
regex::REGEX_MACRO,
Expand Down
68 changes: 68 additions & 0 deletions clippy_lints/src/redundant_field_names.rs
@@ -0,0 +1,68 @@
use rustc::lint::*;
use rustc::hir::*;
use utils::{span_lint_and_sugg};

/// **What it does:** Checks for redundnat field names where shorthands
/// can be used.
///
/// **Why is this bad?** If the field and variable names are the same,
/// the field name is redundant.
///
/// **Known problems:** None.
///
/// **Example:**
/// ```rust
/// let bar: u8 = 123;
///
/// struct Foo {
/// bar: u8,
/// }
///
/// let foo = Foo{ bar: bar }
/// ```
declare_lint! {
pub REDUNDANT_FIELD_NAMES,
Warn,
"using same name for field and variable ,where shorthand can be used"
}

pub struct RedundantFieldNames;

impl LintPass for RedundantFieldNames {
fn get_lints(&self) -> LintArray {
lint_array!(REDUNDANT_FIELD_NAMES)
}
}

impl<'a, 'tcx> LateLintPass<'a, 'tcx> for RedundantFieldNames {
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
if let ExprStruct(_, ref fields, _) = expr.node {
for field in fields {
let name = field.name.node;
if let ExprPath(ref qpath) = field.expr.node {
if let &QPath::Resolved(_, ref path) = qpath {
let segments = &path.segments;

if segments.len() == 1 {
let expr_name = segments[0].name;

if name == expr_name {
span_lint_and_sugg(
cx,
REDUNDANT_FIELD_NAMES,
path.span,
"redundant field names in struct initialization",
&format!(
"replace '{0}: {0}' with '{0}'",
name,
),
"".to_string()
);
}
}
}
}
}
}
}
}
1 change: 1 addition & 0 deletions tests/ui/no_effect.rs
Expand Up @@ -5,6 +5,7 @@
#![allow(dead_code)]
#![allow(path_statements)]
#![allow(deref_addrof)]
#![allow(redundant_field_names)]
#![feature(untagged_unions)]

struct Unit;
Expand Down

0 comments on commit 85642dd

Please sign in to comment.