4 use utils::{match_qpath, match_var, span_lint_and_sugg};
7 /// **What it does:** Checks for fields in struct literals where shorthands
10 /// **Why is this bad?** If the field and variable names are the same,
11 /// the field name is redundant.
13 /// **Known problems:** None.
17 /// let bar: u8 = 123;
23 /// let foo = Foo{ bar: bar }
26 pub REDUNDANT_FIELD_NAMES,
28 "checks for fields in struct literals where shorthands could be used"
31 pub struct RedundantFieldNames;
33 impl LintPass for RedundantFieldNames {
34 fn get_lints(&self) -> LintArray {
35 lint_array!(REDUNDANT_FIELD_NAMES)
39 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for RedundantFieldNames {
40 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
41 if let ExprStruct(ref path, ref fields, _) = expr.node {
43 let name = field.name.node;
45 if is_range_struct_field(path, &name) {
49 if match_var(&field.expr, name) && !field.is_shorthand {
52 REDUNDANT_FIELD_NAMES,
54 "redundant field names in struct initialization",
71 /// let _ = start..end;
73 fn is_range_struct_field(path: &QPath, name: &Name) -> bool {
74 match name.as_str().as_ref() {
76 match_qpath(path, &paths::RANGE_STD) || match_qpath(path, &paths::RANGE_FROM_STD)
77 || match_qpath(path, &paths::RANGE_INCLUSIVE_STD)
80 match_qpath(path, &paths::RANGE_STD) || match_qpath(path, &paths::RANGE_TO_STD)
81 || match_qpath(path, &paths::RANGE_INCLUSIVE_STD)
82 || match_qpath(path, &paths::RANGE_TO_INCLUSIVE_STD)