]> git.lizzy.rs Git - rust.git/commitdiff
Merge pull request #2519 from ordovicia/issue-2516
authorOliver Schneider <oli-obk@users.noreply.github.com>
Sun, 11 Mar 2018 13:04:28 +0000 (14:04 +0100)
committerGitHub <noreply@github.com>
Sun, 11 Mar 2018 13:04:28 +0000 (14:04 +0100)
Fix #2516

Cargo.toml
clippy_lints/src/redundant_field_names.rs
tests/ui/redundant_field_names.rs
tests/ui/redundant_field_names.stderr

index 802e7733985743a6f7c5b7aa74bb07f94e638dac..36457023d9280a0664c7912dc2401cca418cb9ac 100644 (file)
@@ -49,6 +49,7 @@ lazy_static = "1.0"
 serde_derive = "1.0"
 clippy-mini-macro-test = { version = "0.2", path = "mini-macro" }
 serde = "1.0"
+derive-new = "0.5"
 
 [features]
 debugging = []
index 6775129f9df49fb11117159a531aa80bca294f9e..a63447575ef6db9f8cc68bddb925039125ed7f0f 100644 (file)
@@ -1,6 +1,6 @@
 use rustc::lint::*;
 use rustc::hir::*;
-use utils::{is_range_expression, match_var, span_lint_and_sugg};
+use utils::{in_macro, is_range_expression, match_var, span_lint_and_sugg};
 
 /// **What it does:** Checks for fields in struct literals where shorthands
 /// could be used.
@@ -36,10 +36,10 @@ fn get_lints(&self) -> LintArray {
 
 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for RedundantFieldNames {
     fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
-        // Do not care about range expressions.
-        // They could have redundant field name when desugared to structs.
-        // e.g. `start..end` is desugared to `Range { start: start, end: end }`
-        if is_range_expression(expr.span) {
+        // 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) || is_range_expression(expr.span) {
             return;
         }
 
index cb49283010ba0ca03c78b0a626f9a727369eb246..98b6e16c450c4d28cf6b060982045a107c5b68ed 100644 (file)
@@ -2,6 +2,9 @@
 #![allow(unused_variables)]
 #![feature(inclusive_range, inclusive_range_syntax)]
 
+#[macro_use]
+extern crate derive_new;
+
 use std::ops::{Range, RangeFrom, RangeTo, RangeInclusive, RangeToInclusive};
 
 mod foo {
@@ -16,6 +19,11 @@ struct Person {
     foo: u8,
 }
 
+#[derive(new)]
+pub struct S {
+    v: String,
+}
+
 fn main() {
     let gender: u8 = 42;
     let age = 0;
index 40315c6ffac2b3b48664c867d9ff377bef22dc16..91db8a5f0d16c628d125d1125753b3d2716606eb 100644 (file)
@@ -1,57 +1,57 @@
 error: redundant field names in struct initialization
-  --> $DIR/redundant_field_names.rs:26:9
+  --> $DIR/redundant_field_names.rs:34:9
    |
-26 |         gender: gender,
+34 |         gender: gender,
    |         ^^^^^^^^^^^^^^ help: replace it with: `gender`
    |
    = note: `-D redundant-field-names` implied by `-D warnings`
 
 error: redundant field names in struct initialization
-  --> $DIR/redundant_field_names.rs:27:9
+  --> $DIR/redundant_field_names.rs:35:9
    |
-27 |         age: age,
+35 |         age: age,
    |         ^^^^^^^^ help: replace it with: `age`
 
 error: redundant field names in struct initialization
-  --> $DIR/redundant_field_names.rs:45:25
+  --> $DIR/redundant_field_names.rs:53:25
    |
-45 |     let _ = RangeFrom { start: start };
+53 |     let _ = RangeFrom { start: start };
    |                         ^^^^^^^^^^^^ help: replace it with: `start`
 
 error: redundant field names in struct initialization
-  --> $DIR/redundant_field_names.rs:46:23
+  --> $DIR/redundant_field_names.rs:54:23
    |
-46 |     let _ = RangeTo { end: end };
+54 |     let _ = RangeTo { end: end };
    |                       ^^^^^^^^ help: replace it with: `end`
 
 error: redundant field names in struct initialization
-  --> $DIR/redundant_field_names.rs:47:21
+  --> $DIR/redundant_field_names.rs:55:21
    |
-47 |     let _ = Range { start: start, end: end };
+55 |     let _ = Range { start: start, end: end };
    |                     ^^^^^^^^^^^^ help: replace it with: `start`
 
 error: redundant field names in struct initialization
-  --> $DIR/redundant_field_names.rs:47:35
+  --> $DIR/redundant_field_names.rs:55:35
    |
-47 |     let _ = Range { start: start, end: end };
+55 |     let _ = Range { start: start, end: end };
    |                                   ^^^^^^^^ help: replace it with: `end`
 
 error: redundant field names in struct initialization
-  --> $DIR/redundant_field_names.rs:48:30
+  --> $DIR/redundant_field_names.rs:56:30
    |
-48 |     let _ = RangeInclusive { start: start, end: end };
+56 |     let _ = RangeInclusive { start: start, end: end };
    |                              ^^^^^^^^^^^^ help: replace it with: `start`
 
 error: redundant field names in struct initialization
-  --> $DIR/redundant_field_names.rs:48:44
+  --> $DIR/redundant_field_names.rs:56:44
    |
-48 |     let _ = RangeInclusive { start: start, end: end };
+56 |     let _ = RangeInclusive { start: start, end: end };
    |                                            ^^^^^^^^ help: replace it with: `end`
 
 error: redundant field names in struct initialization
-  --> $DIR/redundant_field_names.rs:49:32
+  --> $DIR/redundant_field_names.rs:57:32
    |
-49 |     let _ = RangeToInclusive { end: end };
+57 |     let _ = RangeToInclusive { end: end };
    |                                ^^^^^^^^ help: replace it with: `end`
 
 error: aborting due to 9 previous errors