Skip to content

Commit ab42ba4

Browse files
committed
Implement use_self for tuple structs
1 parent 0f3dcdc commit ab42ba4

File tree

3 files changed

+27
-6
lines changed

3 files changed

+27
-6
lines changed

clippy_lints/src/use_self.rs

Lines changed: 10 additions & 5 deletions
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};
@@ -30,8 +31,7 @@ use syntax_pos::symbol::keywords::SelfUpper;
3031
/// - False positive when using associated types (#2843)
3132
/// - False positives in some situations when using generics (#3410)
3233
/// - False positive when type from outer function can't be used (#3463)
33-
/// - Does not diagnose tuple structs (#3498)
34-
/// - Does not trigger in lifetimed struct
34+
/// - Does not trigger in lifetimed structs
3535
///
3636
/// **Example:**
3737
/// ```rust
@@ -232,10 +232,15 @@ struct UseSelfVisitor<'a, 'tcx: 'a> {
232232

233233
impl<'a, 'tcx> Visitor<'tcx> for UseSelfVisitor<'a, 'tcx> {
234234
fn visit_path(&mut self, path: &'tcx Path, _id: HirId) {
235-
if self.item_path.def == path.def && path.segments.last().expect(SEGMENTS_MSG).ident.name != SelfUpper.name() {
236-
span_use_self_lint(self.cx, path);
235+
if path.segments.last().expect(SEGMENTS_MSG).ident.name != SelfUpper.name() {
236+
if self.item_path.def == path.def {
237+
span_use_self_lint(self.cx, path);
238+
} else if let Def::StructCtor(ctor_did, CtorKind::Fn) = path.def {
239+
if self.item_path.def.opt_def_id() == self.cx.tcx.parent_def_id(ctor_did) {
240+
span_use_self_lint(self.cx, path);
241+
}
242+
}
237243
}
238-
239244
walk_path(self, path);
240245
}
241246

tests/ui/use_self.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,16 @@ mod existential {
216216
}
217217
}
218218

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+
219229
mod issue3410 {
220230

221231
struct A;

tests/ui/use_self.stderr

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,5 +126,11 @@ error: unnecessary structure name repetition
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+
224 | TS(0)
133+
| ^^ help: use the applicable keyword: `Self`
134+
135+
error: aborting due to 22 previous errors
130136

0 commit comments

Comments
 (0)