Skip to content

Commit 7b59557

Browse files
committed
Don't lint range syntax with var name start and end
1 parent e5f2d62 commit 7b59557

File tree

3 files changed

+54
-6
lines changed

3 files changed

+54
-6
lines changed

clippy_lints/src/redundant_field_names.rs

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
use syntax::ast::Name;
12
use rustc::lint::*;
23
use rustc::hir::*;
3-
use utils::{span_lint_and_sugg, match_var};
4+
use utils::{match_qpath, match_var, span_lint_and_sugg};
5+
use utils::paths;
46

57
/// **What it does:** Checks for fields in struct literals where shorthands
68
/// could be used.
@@ -36,10 +38,14 @@ impl LintPass for RedundantFieldNames {
3638

3739
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for RedundantFieldNames {
3840
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
39-
if let ExprStruct(_, ref fields, _) = expr.node {
41+
if let ExprStruct(ref path, ref fields, _) = expr.node {
4042
for field in fields {
4143
let name = field.name.node;
4244

45+
if is_range_struct_field(path, &name) {
46+
continue;
47+
}
48+
4349
if match_var(&field.expr, name) && !field.is_shorthand {
4450
span_lint_and_sugg (
4551
cx,
@@ -54,3 +60,27 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for RedundantFieldNames {
5460
}
5561
}
5662
}
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+
}

tests/ui/redundant_field_names.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#![warn(redundant_field_names)]
22
#![allow(unused_variables)]
3+
#![feature(inclusive_range,inclusive_range_syntax)]
34

45
mod foo {
56
pub const BAR: u8 = 0;
@@ -27,4 +28,21 @@ fn main() {
2728
buzz: fizz, //should be ok
2829
foo: foo::BAR, //should be ok
2930
};
31+
32+
// Range syntax
33+
let (start, end) = (0, 0);
34+
35+
let _ = start..;
36+
let _ = ..end;
37+
let _ = start..end;
38+
39+
let _ = ..=end;
40+
let _ = start..=end;
41+
42+
// TODO: the followings shoule 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 };
3048
}

tests/ui/redundant_field_names.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
error: redundant field names in struct initialization
2-
--> $DIR/redundant_field_names.rs:23:9
2+
--> $DIR/redundant_field_names.rs:24:9
33
|
4-
23 | gender: gender,
4+
24 | 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:25:9
1111
|
12-
24 | age: age,
12+
25 | age: age,
1313
| ^^^^^^^^ help: replace it with: `age`
1414

1515
error: aborting due to 2 previous errors

0 commit comments

Comments
 (0)