Skip to content

Commit 2e0977f

Browse files
committed
Fixed potential mistakes with nesting. Added tests.
1 parent 13b5ea4 commit 2e0977f

File tree

3 files changed

+50
-6
lines changed

3 files changed

+50
-6
lines changed

clippy_lints/src/use_self.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use rustc::lint::{in_external_macro, LateContext, LateLintPass, LintArray, LintC
77
use rustc::ty;
88
use rustc::{declare_tool_lint, lint_array};
99
use rustc_errors::Applicability;
10-
use syntax_pos::{symbol::keywords::SelfUpper, Span};
10+
use syntax_pos::symbol::keywords::SelfUpper;
1111

1212
/// **What it does:** Checks for unnecessary repetition of structure name when a
1313
/// replacement with `Self` is applicable.
@@ -55,7 +55,13 @@ impl LintPass for UseSelf {
5555

5656
const SEGMENTS_MSG: &str = "segments should be composed of at least 1 element";
5757

58-
fn span_use_self_lint(cx: &LateContext<'_, '_>, span: Span) {
58+
fn span_use_self_lint(cx: &LateContext<'_, '_>, path: &Path) {
59+
// path segments only include actual path, no methods or fields
60+
let last_path_span = path.segments.last().expect(SEGMENTS_MSG).ident.span;
61+
// `to()` doesn't shorten span, so we shorten it with `until(..)`
62+
// and then include it with `to(..)`
63+
let span = path.span.until(last_path_span).to(last_path_span);
64+
5965
span_lint_and_sugg(
6066
cx,
6167
USE_SELF,
@@ -92,7 +98,7 @@ impl<'a, 'tcx> Visitor<'tcx> for TraitImplTyVisitor<'a, 'tcx> {
9298
};
9399

94100
if !is_self_ty {
95-
span_use_self_lint(self.cx, path.span);
101+
span_use_self_lint(self.cx, path);
96102
}
97103
}
98104
}
@@ -221,10 +227,10 @@ impl<'a, 'tcx> Visitor<'tcx> for UseSelfVisitor<'a, 'tcx> {
221227
fn visit_path(&mut self, path: &'tcx Path, _id: HirId) {
222228
if path.segments.last().expect(SEGMENTS_MSG).ident.name != SelfUpper.name() {
223229
if self.item_path.def == path.def {
224-
span_use_self_lint(self.cx, path.segments.first().expect(SEGMENTS_MSG).ident.span);
230+
span_use_self_lint(self.cx, path);
225231
} else if let Def::StructCtor(ctor_did, CtorKind::Fn) = path.def {
226232
if self.item_path.def.opt_def_id() == self.cx.tcx.parent_def_id(ctor_did) {
227-
span_use_self_lint(self.cx, path.span);
233+
span_use_self_lint(self.cx, path);
228234
}
229235
}
230236
}

tests/ui/use_self.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,3 +274,23 @@ mod issue3410 {
274274
fn a(_: Vec<A>) {}
275275
}
276276
}
277+
278+
#[allow(clippy::no_effect)]
279+
mod rustfix {
280+
mod nested {
281+
pub struct A {}
282+
}
283+
284+
impl nested::A {
285+
const A: bool = true;
286+
287+
fn fun_1() {}
288+
289+
fn fun_2() {
290+
nested::A::fun_1();
291+
nested::A::A;
292+
293+
nested::A {};
294+
}
295+
}
296+
}

tests/ui/use_self.stderr

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,5 +162,23 @@ error: unnecessary structure name repetition
162162
LL | Bar { foo: Foo {} }
163163
| ^^^ help: use the applicable keyword: `Self`
164164

165-
error: aborting due to 26 previous errors
165+
error: unnecessary structure name repetition
166+
--> $DIR/use_self.rs:290:13
167+
|
168+
LL | nested::A::fun_1();
169+
| ^^^^^^^^^ help: use the applicable keyword: `Self`
170+
171+
error: unnecessary structure name repetition
172+
--> $DIR/use_self.rs:291:13
173+
|
174+
LL | nested::A::A;
175+
| ^^^^^^^^^ help: use the applicable keyword: `Self`
176+
177+
error: unnecessary structure name repetition
178+
--> $DIR/use_self.rs:293:13
179+
|
180+
LL | nested::A {};
181+
| ^^^^^^^^^ help: use the applicable keyword: `Self`
182+
183+
error: aborting due to 29 previous errors
166184

0 commit comments

Comments
 (0)