Skip to content

Commit 735607c

Browse files
committed
Auto merge of #3588 - detrumi:tuple_struct_use_self, r=phansch
`use_self` for tuple structs Fixes #3498
2 parents aee138a + 259ec2d commit 735607c

File tree

3 files changed

+47
-23
lines changed

3 files changed

+47
-23
lines changed

clippy_lints/src/use_self.rs

+13-4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
use crate::utils::{in_macro, span_lint_and_sugg};
1111
use if_chain::if_chain;
12+
use rustc::hir::def::{CtorKind, Def};
1213
use rustc::hir::intravisit::{walk_path, walk_ty, NestedVisitorMap, Visitor};
1314
use rustc::hir::*;
1415
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
@@ -25,7 +26,10 @@ use syntax_pos::symbol::keywords::SelfUpper;
2526
/// name
2627
/// feels inconsistent.
2728
///
28-
/// **Known problems:** None.
29+
/// **Known problems:**
30+
/// - False positive when using associated types (#2843)
31+
/// - False positives in some situations when using generics (#3410)
32+
/// - False positive when type from outer function can't be used (#3463)
2933
///
3034
/// **Example:**
3135
/// ```rust
@@ -226,10 +230,15 @@ struct UseSelfVisitor<'a, 'tcx: 'a> {
226230

227231
impl<'a, 'tcx> Visitor<'tcx> for UseSelfVisitor<'a, 'tcx> {
228232
fn visit_path(&mut self, path: &'tcx Path, _id: HirId) {
229-
if self.item_path.def == path.def && path.segments.last().expect(SEGMENTS_MSG).ident.name != SelfUpper.name() {
230-
span_use_self_lint(self.cx, path);
233+
if path.segments.last().expect(SEGMENTS_MSG).ident.name != SelfUpper.name() {
234+
if self.item_path.def == path.def {
235+
span_use_self_lint(self.cx, path);
236+
} else if let Def::StructCtor(ctor_did, CtorKind::Fn) = path.def {
237+
if self.item_path.def.opt_def_id() == self.cx.tcx.parent_def_id(ctor_did) {
238+
span_use_self_lint(self.cx, path);
239+
}
240+
}
231241
}
232-
233242
walk_path(self, path);
234243
}
235244

tests/ui/use_self.rs

+12-3
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,6 @@ mod better {
5151
}
5252
}
5353

54-
//todo the lint does not handle lifetimed struct
55-
//the following module should trigger the lint on the third method only
5654
mod lifetimes {
5755
struct Foo<'a> {
5856
foo_str: &'a str,
@@ -69,7 +67,8 @@ mod lifetimes {
6967
Foo { foo_str: "foo" }
7068
}
7169

72-
// `Self` is applicable here
70+
// FIXME: the lint does not handle lifetimed struct
71+
// `Self` should be applicable here
7372
fn clone(&self) -> Foo<'a> {
7473
Foo { foo_str: self.foo_str }
7574
}
@@ -217,6 +216,16 @@ mod existential {
217216
}
218217
}
219218

219+
mod tuple_structs {
220+
pub struct TS(i32);
221+
222+
impl TS {
223+
pub fn ts() -> Self {
224+
TS(0)
225+
}
226+
}
227+
}
228+
220229
mod issue3410 {
221230

222231
struct A;

tests/ui/use_self.stderr

+22-16
Original file line numberDiff line numberDiff line change
@@ -37,94 +37,100 @@ LL | Foo::new()
3737
| ^^^^^^^^ help: use the applicable keyword: `Self`
3838

3939
error: unnecessary structure name repetition
40-
--> $DIR/use_self.rs:96:22
40+
--> $DIR/use_self.rs:95:22
4141
|
4242
LL | fn refs(p1: &Bad) -> &Bad {
4343
| ^^^ help: use the applicable keyword: `Self`
4444

4545
error: unnecessary structure name repetition
46-
--> $DIR/use_self.rs:96:31
46+
--> $DIR/use_self.rs:95:31
4747
|
4848
LL | fn refs(p1: &Bad) -> &Bad {
4949
| ^^^ help: use the applicable keyword: `Self`
5050

5151
error: unnecessary structure name repetition
52-
--> $DIR/use_self.rs:100:37
52+
--> $DIR/use_self.rs:99:37
5353
|
5454
LL | fn ref_refs<'a>(p1: &'a &'a Bad) -> &'a &'a Bad {
5555
| ^^^ help: use the applicable keyword: `Self`
5656

5757
error: unnecessary structure name repetition
58-
--> $DIR/use_self.rs:100:53
58+
--> $DIR/use_self.rs:99:53
5959
|
6060
LL | fn ref_refs<'a>(p1: &'a &'a Bad) -> &'a &'a Bad {
6161
| ^^^ help: use the applicable keyword: `Self`
6262

6363
error: unnecessary structure name repetition
64-
--> $DIR/use_self.rs:104:30
64+
--> $DIR/use_self.rs:103:30
6565
|
6666
LL | fn mut_refs(p1: &mut Bad) -> &mut Bad {
6767
| ^^^ help: use the applicable keyword: `Self`
6868

6969
error: unnecessary structure name repetition
70-
--> $DIR/use_self.rs:104:43
70+
--> $DIR/use_self.rs:103:43
7171
|
7272
LL | fn mut_refs(p1: &mut Bad) -> &mut Bad {
7373
| ^^^ help: use the applicable keyword: `Self`
7474

7575
error: unnecessary structure name repetition
76-
--> $DIR/use_self.rs:108:28
76+
--> $DIR/use_self.rs:107:28
7777
|
7878
LL | fn nested(_p1: Box<Bad>, _p2: (&u8, &Bad)) {}
7979
| ^^^ help: use the applicable keyword: `Self`
8080

8181
error: unnecessary structure name repetition
82-
--> $DIR/use_self.rs:108:46
82+
--> $DIR/use_self.rs:107:46
8383
|
8484
LL | fn nested(_p1: Box<Bad>, _p2: (&u8, &Bad)) {}
8585
| ^^^ help: use the applicable keyword: `Self`
8686

8787
error: unnecessary structure name repetition
88-
--> $DIR/use_self.rs:110:20
88+
--> $DIR/use_self.rs:109:20
8989
|
9090
LL | fn vals(_: Bad) -> Bad {
9191
| ^^^ help: use the applicable keyword: `Self`
9292

9393
error: unnecessary structure name repetition
94-
--> $DIR/use_self.rs:110:28
94+
--> $DIR/use_self.rs:109:28
9595
|
9696
LL | fn vals(_: Bad) -> Bad {
9797
| ^^^ help: use the applicable keyword: `Self`
9898

9999
error: unnecessary structure name repetition
100-
--> $DIR/use_self.rs:111:13
100+
--> $DIR/use_self.rs:110:13
101101
|
102102
LL | Bad::default()
103103
| ^^^^^^^^^^^^ help: use the applicable keyword: `Self`
104104

105105
error: unnecessary structure name repetition
106-
--> $DIR/use_self.rs:116:23
106+
--> $DIR/use_self.rs:115:23
107107
|
108108
LL | type Output = Bad;
109109
| ^^^ help: use the applicable keyword: `Self`
110110

111111
error: unnecessary structure name repetition
112-
--> $DIR/use_self.rs:118:27
112+
--> $DIR/use_self.rs:117:27
113113
|
114114
LL | fn mul(self, rhs: Bad) -> Bad {
115115
| ^^^ help: use the applicable keyword: `Self`
116116

117117
error: unnecessary structure name repetition
118-
--> $DIR/use_self.rs:118:35
118+
--> $DIR/use_self.rs:117:35
119119
|
120120
LL | fn mul(self, rhs: Bad) -> Bad {
121121
| ^^^ help: use the applicable keyword: `Self`
122122

123123
error: unnecessary structure name repetition
124-
--> $DIR/use_self.rs:210:56
124+
--> $DIR/use_self.rs:209:56
125125
|
126126
LL | fn bad(foos: &[Self]) -> impl Iterator<Item = &Foo> {
127127
| ^^^ help: use the applicable keyword: `Self`
128128

129-
error: aborting due to 21 previous errors
129+
error: unnecessary structure name repetition
130+
--> $DIR/use_self.rs:224:13
131+
|
132+
LL | TS(0)
133+
| ^^ help: use the applicable keyword: `Self`
134+
135+
error: aborting due to 22 previous errors
130136

0 commit comments

Comments
 (0)