Skip to content

Commit 3900223

Browse files
authored
Merge pull request #1559 from theotherphil/master
Use span of impl/trait in len_without_is_empty error message, rather …
2 parents 7c1efc6 + d6a4d2c commit 3900223

File tree

3 files changed

+75
-34
lines changed

3 files changed

+75
-34
lines changed

clippy_lints/src/len_zero.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ fn check_trait_items(cx: &LateContext, item: &Item, trait_items: &[TraitItemRef]
107107
if cx.access_levels.is_exported(i.id.node_id) {
108108
span_lint(cx,
109109
LEN_WITHOUT_IS_EMPTY,
110-
i.span,
110+
item.span,
111111
&format!("trait `{}` has a `len` method but no `is_empty` method", item.name));
112112
}
113113
}
@@ -146,7 +146,7 @@ fn check_impl_items(cx: &LateContext, item: &Item, impl_items: &[ImplItemRef]) {
146146

147147
span_lint(cx,
148148
LEN_WITHOUT_IS_EMPTY,
149-
i.span,
149+
item.span,
150150
&format!("item `{}` has a public `len` method but {} `is_empty` method", ty, is_empty));
151151
}
152152
}

tests/ui/len_zero.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,30 @@ impl PubOne {
1212
}
1313
}
1414

15+
impl PubOne { // A second impl for this struct - the error span shouldn't mention this
16+
pub fn irrelevant(self: &Self) -> bool {
17+
false
18+
}
19+
}
20+
21+
// Identical to PubOne, but with an allow attribute on the impl complaining len
22+
pub struct PubAllowed;
23+
24+
#[allow(len_without_is_empty)]
25+
impl PubAllowed {
26+
pub fn len(self: &Self) -> isize {
27+
1
28+
}
29+
}
30+
31+
// No allow attribute on this impl block, but that doesn't matter - we only require one on the
32+
// impl containing len.
33+
impl PubAllowed {
34+
pub fn irrelevant(self: &Self) -> bool {
35+
false
36+
}
37+
}
38+
1539
struct NotPubOne;
1640

1741
impl NotPubOne {

tests/ui/len_zero.stderr

Lines changed: 49 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
error: item `PubOne` has a public `len` method but no corresponding `is_empty` method
2-
--> $DIR/len_zero.rs:10:5
2+
--> $DIR/len_zero.rs:9:1
33
|
4-
10 | pub fn len(self: &Self) -> isize {
5-
| _____^ starting here...
4+
9 | impl PubOne {
5+
| _^ starting here...
6+
10 | | pub fn len(self: &Self) -> isize {
67
11 | | 1
78
12 | | }
8-
| |_____^ ...ending here
9+
13 | | }
10+
| |_^ ...ending here
911
|
1012
note: lint level defined here
1113
--> $DIR/len_zero.rs:4:9
@@ -14,33 +16,48 @@ note: lint level defined here
1416
| ^^^^^^^^^^^^^^^^^^^^
1517

1618
error: trait `PubTraitsToo` has a `len` method but no `is_empty` method
17-
--> $DIR/len_zero.rs:32:5
19+
--> $DIR/len_zero.rs:55:1
1820
|
19-
32 | fn len(self: &Self) -> isize;
20-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
21+
55 | pub trait PubTraitsToo {
22+
| _^ starting here...
23+
56 | | fn len(self: &Self) -> isize;
24+
57 | | }
25+
| |_^ ...ending here
2126

2227
error: item `HasIsEmpty` has a public `len` method but a private `is_empty` method
23-
--> $DIR/len_zero.rs:66:5
28+
--> $DIR/len_zero.rs:89:1
2429
|
25-
66 | pub fn len(self: &Self) -> isize {
26-
| _____^ starting here...
27-
67 | | 1
28-
68 | | }
29-
| |_____^ ...ending here
30+
89 | impl HasIsEmpty {
31+
| _^ starting here...
32+
90 | | pub fn len(self: &Self) -> isize {
33+
91 | | 1
34+
92 | | }
35+
93 | |
36+
94 | | fn is_empty(self: &Self) -> bool {
37+
95 | | false
38+
96 | | }
39+
97 | | }
40+
| |_^ ...ending here
3041

3142
error: item `HasWrongIsEmpty` has a public `len` method but no corresponding `is_empty` method
32-
--> $DIR/len_zero.rs:95:5
33-
|
34-
95 | pub fn len(self: &Self) -> isize {
35-
| _____^ starting here...
36-
96 | | 1
37-
97 | | }
38-
| |_____^ ...ending here
43+
--> $DIR/len_zero.rs:118:1
44+
|
45+
118 | impl HasWrongIsEmpty {
46+
| _^ starting here...
47+
119 | | pub fn len(self: &Self) -> isize {
48+
120 | | 1
49+
121 | | }
50+
122 | |
51+
123 | | pub fn is_empty(self: &Self, x : u32) -> bool {
52+
124 | | false
53+
125 | | }
54+
126 | | }
55+
| |_^ ...ending here
3956

4057
error: length comparison to zero
41-
--> $DIR/len_zero.rs:106:8
58+
--> $DIR/len_zero.rs:130:8
4259
|
43-
106 | if x.len() == 0 {
60+
130 | if x.len() == 0 {
4461
| ^^^^^^^^^^^^
4562
|
4663
note: lint level defined here
@@ -52,45 +69,45 @@ help: consider using `is_empty`
5269
| if x.is_empty() {
5370

5471
error: length comparison to zero
55-
--> $DIR/len_zero.rs:113:8
72+
--> $DIR/len_zero.rs:137:8
5673
|
57-
113 | if "".len() == 0 {
74+
137 | if "".len() == 0 {
5875
| ^^^^^^^^^^^^^
5976
|
6077
help: consider using `is_empty`
6178
| if "".is_empty() {
6279

6380
error: length comparison to zero
64-
--> $DIR/len_zero.rs:130:8
81+
--> $DIR/len_zero.rs:154:8
6582
|
66-
130 | if has_is_empty.len() == 0 {
83+
154 | if has_is_empty.len() == 0 {
6784
| ^^^^^^^^^^^^^^^^^^^^^^^
6885
|
6986
help: consider using `is_empty`
7087
| if has_is_empty.is_empty() {
7188

7289
error: length comparison to zero
73-
--> $DIR/len_zero.rs:136:8
90+
--> $DIR/len_zero.rs:160:8
7491
|
75-
136 | if has_is_empty.len() != 0 {
92+
160 | if has_is_empty.len() != 0 {
7693
| ^^^^^^^^^^^^^^^^^^^^^^^
7794
|
7895
help: consider using `is_empty`
7996
| if !has_is_empty.is_empty() {
8097

8198
error: length comparison to zero
82-
--> $DIR/len_zero.rs:142:8
99+
--> $DIR/len_zero.rs:166:8
83100
|
84-
142 | if has_is_empty.len() > 0 {
101+
166 | if has_is_empty.len() > 0 {
85102
| ^^^^^^^^^^^^^^^^^^^^^^
86103
|
87104
help: consider using `is_empty`
88105
| if !has_is_empty.is_empty() {
89106

90107
error: length comparison to zero
91-
--> $DIR/len_zero.rs:151:8
108+
--> $DIR/len_zero.rs:175:8
92109
|
93-
151 | if with_is_empty.len() == 0 {
110+
175 | if with_is_empty.len() == 0 {
94111
| ^^^^^^^^^^^^^^^^^^^^^^^^
95112
|
96113
help: consider using `is_empty`

0 commit comments

Comments
 (0)