Skip to content

Commit d264e40

Browse files
committed
Auto merge of #3627 - detrumi:use_self_local_macro, r=phansch
Trigger `use_self` lint in local macros Closes #2098 The test currently only covers local macros. #2098 suggested this: > You could add the macro in question into the `mini_macro` subcrate But that doesn't work for a `macro_rules`: ``` error: cannot export macro_rules! macros from a `proc-macro` crate type currently ``` So I suggest leaving out the test for external macros, as using `in_external_macro` seems straigtforward enough. Alternatives would be to use to add an additional crate (overkill if you ask me), or test with a `proc-macro`.
2 parents 3bcf67c + 407ff74 commit d264e40

File tree

3 files changed

+38
-4
lines changed

3 files changed

+38
-4
lines changed

clippy_lints/src/use_self.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
// option. This file may not be copied, modified, or distributed
88
// except according to those terms.
99

10-
use crate::utils::{in_macro, span_lint_and_sugg};
10+
use crate::utils::span_lint_and_sugg;
1111
use if_chain::if_chain;
1212
use rustc::hir::def::{CtorKind, Def};
1313
use rustc::hir::intravisit::{walk_path, walk_ty, NestedVisitorMap, Visitor};
1414
use rustc::hir::*;
15-
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
15+
use rustc::lint::{in_external_macro, LateContext, LateLintPass, LintArray, LintContext, LintPass};
1616
use rustc::ty;
1717
use rustc::{declare_tool_lint, lint_array};
1818
use rustc_errors::Applicability;
@@ -172,7 +172,7 @@ fn check_trait_method_impl_decl<'a, 'tcx: 'a>(
172172

173173
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UseSelf {
174174
fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx Item) {
175-
if in_macro(item.span) {
175+
if in_external_macro(cx.sess(), item.span) {
176176
return;
177177
}
178178
if_chain! {

tests/ui/use_self.rs

+16
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,22 @@ mod tuple_structs {
226226
}
227227
}
228228

229+
mod macros {
230+
macro_rules! use_self_expand {
231+
() => {
232+
fn new() -> Foo {
233+
Foo {}
234+
}
235+
};
236+
}
237+
238+
struct Foo {}
239+
240+
impl Foo {
241+
use_self_expand!(); // Should lint in local macros
242+
}
243+
}
244+
229245
mod issue3410 {
230246

231247
struct A;

tests/ui/use_self.stderr

+19-1
Original file line numberDiff line numberDiff line change
@@ -132,5 +132,23 @@ error: unnecessary structure name repetition
132132
LL | TS(0)
133133
| ^^ help: use the applicable keyword: `Self`
134134

135-
error: aborting due to 22 previous errors
135+
error: unnecessary structure name repetition
136+
--> $DIR/use_self.rs:232:25
137+
|
138+
LL | fn new() -> Foo {
139+
| ^^^ help: use the applicable keyword: `Self`
140+
...
141+
LL | use_self_expand!(); // Should lint in local macros
142+
| ------------------- in this macro invocation
143+
144+
error: unnecessary structure name repetition
145+
--> $DIR/use_self.rs:233:17
146+
|
147+
LL | Foo {}
148+
| ^^^ help: use the applicable keyword: `Self`
149+
...
150+
LL | use_self_expand!(); // Should lint in local macros
151+
| ------------------- in this macro invocation
152+
153+
error: aborting due to 24 previous errors
136154

0 commit comments

Comments
 (0)