Skip to content

Commit 05f92b8

Browse files
authored
Merge pull request #2507 from ordovicia/redundant_field_names_range
Don't lint range syntax with var name `start` and/or `end`
2 parents 40ebff8 + cdb60c6 commit 05f92b8

File tree

4 files changed

+86
-7
lines changed

4 files changed

+86
-7
lines changed

clippy_lints/src/redundant_field_names.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use rustc::lint::*;
22
use rustc::hir::*;
3-
use utils::{span_lint_and_sugg, match_var};
3+
use utils::{is_range_expression, match_var, span_lint_and_sugg};
44

55
/// **What it does:** Checks for fields in struct literals where shorthands
66
/// could be used.
@@ -36,10 +36,17 @@ impl LintPass for RedundantFieldNames {
3636

3737
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for RedundantFieldNames {
3838
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
39-
if let ExprStruct(_, ref fields, _) = expr.node {
39+
if let ExprStruct(ref path, ref fields, _) = expr.node {
4040
for field in fields {
4141
let name = field.name.node;
4242

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) {
47+
continue;
48+
}
49+
4350
if match_var(&field.expr, name) && !field.is_shorthand {
4451
span_lint_and_sugg (
4552
cx,

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

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

47
mod foo {
58
pub const BAR: u8 = 0;
@@ -27,4 +30,21 @@ fn main() {
2730
buzz: fizz, //should be ok
2831
foo: foo::BAR, //should be ok
2932
};
33+
34+
// Range expressions
35+
let (start, end) = (0, 0);
36+
37+
let _ = start..;
38+
let _ = ..end;
39+
let _ = start..end;
40+
41+
let _ = ..=end;
42+
let _ = start..=end;
43+
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 };
3050
}

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:23:9
2+
--> $DIR/redundant_field_names.rs:26:9
33
|
4-
23 | 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:24:9
10+
--> $DIR/redundant_field_names.rs:27:9
1111
|
12-
24 | 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)