3 use utils::{is_range_expression, match_var, span_lint_and_sugg};
5 /// **What it does:** Checks for fields in struct literals where shorthands
8 /// **Why is this bad?** If the field and variable names are the same,
9 /// the field name is redundant.
11 /// **Known problems:** None.
15 /// let bar: u8 = 123;
21 /// let foo = Foo{ bar: bar }
24 pub REDUNDANT_FIELD_NAMES,
26 "checks for fields in struct literals where shorthands could be used"
29 pub struct RedundantFieldNames;
31 impl LintPass for RedundantFieldNames {
32 fn get_lints(&self) -> LintArray {
33 lint_array!(REDUNDANT_FIELD_NAMES)
37 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for RedundantFieldNames {
38 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
39 if let ExprStruct(ref path, ref fields, _) = expr.node {
41 let name = field.name.node;
43 // Do not care about range expressions.
44 // They could have redundant field name when desugared to structs.
45 // e.g. `start..end` is desugared to `Range { start: start, end: end }`
46 if is_range_expression(expr.span) {
50 if match_var(&field.expr, name) && !field.is_shorthand {
53 REDUNDANT_FIELD_NAMES,
55 "redundant field names in struct initialization",