Skip to content

Commit cdb60c6

Browse files
committed
Make redundant_field_name not care range expressions
Hand-written `Range` struct family are treated normally.
1 parent 5b48b03 commit cdb60c6

File tree

4 files changed

+72
-41
lines changed

4 files changed

+72
-41
lines changed

clippy_lints/src/redundant_field_names.rs

+5-28
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
use syntax::ast::Name;
21
use rustc::lint::*;
32
use rustc::hir::*;
4-
use utils::{match_qpath, match_var, span_lint_and_sugg};
5-
use utils::paths;
3+
use utils::{is_range_expression, match_var, span_lint_and_sugg};
64

75
/// **What it does:** Checks for fields in struct literals where shorthands
86
/// could be used.
@@ -42,7 +40,10 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for RedundantFieldNames {
4240
for field in fields {
4341
let name = field.name.node;
4442

45-
if is_range_struct_field(path, &name) {
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) {
4647
continue;
4748
}
4849

@@ -60,27 +61,3 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for RedundantFieldNames {
6061
}
6162
}
6263
}
63-
64-
/// ```rust
65-
/// let start = 0;
66-
/// let _ = start..;
67-
///
68-
/// let end = 0;
69-
/// let _ = ..end;
70-
///
71-
/// let _ = start..end;
72-
/// ```
73-
fn is_range_struct_field(path: &QPath, name: &Name) -> bool {
74-
match name.as_str().as_ref() {
75-
"start" => {
76-
match_qpath(path, &paths::RANGE_STD) || match_qpath(path, &paths::RANGE_FROM_STD)
77-
|| match_qpath(path, &paths::RANGE_INCLUSIVE_STD)
78-
},
79-
"end" => {
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)
83-
},
84-
_ => false,
85-
}
86-
}

clippy_lints/src/utils/mod.rs

+10
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,16 @@ pub fn in_macro(span: Span) -> bool {
6464
})
6565
}
6666

67+
/// Returns true if `expn_info` was expanded by range expressions.
68+
pub fn is_range_expression(span: Span) -> bool {
69+
span.ctxt().outer().expn_info().map_or(false, |info| {
70+
match info.callee.format {
71+
ExpnFormat::CompilerDesugaring(CompilerDesugaringKind::DotFill) => true,
72+
_ => false,
73+
}
74+
})
75+
}
76+
6777
/// Returns true if the macro that expanded the crate was outside of the
6878
/// current crate or was a
6979
/// compiler plugin.

tests/ui/redundant_field_names.rs

+10-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#![warn(redundant_field_names)]
22
#![allow(unused_variables)]
3-
#![feature(inclusive_range,inclusive_range_syntax)]
3+
#![feature(inclusive_range, inclusive_range_syntax)]
4+
5+
use std::ops::{Range, RangeFrom, RangeTo, RangeInclusive, RangeToInclusive};
46

57
mod foo {
68
pub const BAR: u8 = 0;
@@ -29,7 +31,7 @@ fn main() {
2931
foo: foo::BAR, //should be ok
3032
};
3133

32-
// Range syntax
34+
// Range expressions
3335
let (start, end) = (0, 0);
3436

3537
let _ = start..;
@@ -39,10 +41,10 @@ fn main() {
3941
let _ = ..=end;
4042
let _ = start..=end;
4143

42-
// TODO: the following should be linted
43-
let _ = ::std::ops::RangeFrom { start: start };
44-
let _ = ::std::ops::RangeTo { end: end };
45-
let _ = ::std::ops::Range { start: start, end: end };
46-
let _ = ::std::ops::RangeInclusive { start: start, end: end };
47-
let _ = ::std::ops::RangeToInclusive { end: end };
44+
// hand-written Range family structs are linted
45+
let _ = RangeFrom { start: start };
46+
let _ = RangeTo { end: end };
47+
let _ = Range { start: start, end: end };
48+
let _ = RangeInclusive { start: start, end: end };
49+
let _ = RangeToInclusive { end: end };
4850
}

tests/ui/redundant_field_names.stderr

+47-5
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,58 @@
11
error: redundant field names in struct initialization
2-
--> $DIR/redundant_field_names.rs:24:9
2+
--> $DIR/redundant_field_names.rs:26:9
33
|
4-
24 | gender: gender,
4+
26 | gender: gender,
55
| ^^^^^^^^^^^^^^ help: replace it with: `gender`
66
|
77
= note: `-D redundant-field-names` implied by `-D warnings`
88

99
error: redundant field names in struct initialization
10-
--> $DIR/redundant_field_names.rs:25:9
10+
--> $DIR/redundant_field_names.rs:27:9
1111
|
12-
25 | age: age,
12+
27 | age: age,
1313
| ^^^^^^^^ help: replace it with: `age`
1414

15-
error: aborting due to 2 previous errors
15+
error: redundant field names in struct initialization
16+
--> $DIR/redundant_field_names.rs:45:25
17+
|
18+
45 | let _ = RangeFrom { start: start };
19+
| ^^^^^^^^^^^^ help: replace it with: `start`
20+
21+
error: redundant field names in struct initialization
22+
--> $DIR/redundant_field_names.rs:46:23
23+
|
24+
46 | let _ = RangeTo { end: end };
25+
| ^^^^^^^^ help: replace it with: `end`
26+
27+
error: redundant field names in struct initialization
28+
--> $DIR/redundant_field_names.rs:47:21
29+
|
30+
47 | let _ = Range { start: start, end: end };
31+
| ^^^^^^^^^^^^ help: replace it with: `start`
32+
33+
error: redundant field names in struct initialization
34+
--> $DIR/redundant_field_names.rs:47:35
35+
|
36+
47 | let _ = Range { start: start, end: end };
37+
| ^^^^^^^^ help: replace it with: `end`
38+
39+
error: redundant field names in struct initialization
40+
--> $DIR/redundant_field_names.rs:48:30
41+
|
42+
48 | let _ = RangeInclusive { start: start, end: end };
43+
| ^^^^^^^^^^^^ help: replace it with: `start`
44+
45+
error: redundant field names in struct initialization
46+
--> $DIR/redundant_field_names.rs:48:44
47+
|
48+
48 | let _ = RangeInclusive { start: start, end: end };
49+
| ^^^^^^^^ help: replace it with: `end`
50+
51+
error: redundant field names in struct initialization
52+
--> $DIR/redundant_field_names.rs:49:32
53+
|
54+
49 | let _ = RangeToInclusive { end: end };
55+
| ^^^^^^^^ help: replace it with: `end`
56+
57+
error: aborting due to 9 previous errors
1658

0 commit comments

Comments
 (0)