Skip to content

Commit cd3c649

Browse files
committed
Merge pull request #508 from devonhollowood/underscore_macros
Implement #507
2 parents 4a32445 + b6766a0 commit cd3c649

File tree

4 files changed

+39
-7
lines changed

4 files changed

+39
-7
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ compiletest_rs = "0.0.11"
2424
regex = "*"
2525
regex_macros = "*"
2626
lazy_static = "*"
27+
rustc-serialize = "0.3"
2728

2829
[features]
2930

src/misc.rs

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ use syntax::ptr::P;
33
use rustc_front::hir::*;
44
use reexport::*;
55
use rustc_front::util::{is_comparison_binop, binop_to_string};
6-
use syntax::codemap::{Span, Spanned};
6+
use syntax::codemap::{Span, Spanned, ExpnFormat};
77
use rustc_front::intravisit::FnKind;
88
use rustc::middle::ty;
99
use rustc::middle::const_eval::ConstVal::Float;
1010
use rustc::middle::const_eval::eval_const_expr_partial;
1111
use rustc::middle::const_eval::EvalHint::ExprTypeChecked;
1212

1313
use utils::{get_item_name, match_path, snippet, get_parent_expr, span_lint};
14-
use utils::{span_help_and_lint, in_external_macro, walk_ptrs_ty, is_integer_literal};
14+
use utils::{span_help_and_lint, walk_ptrs_ty, is_integer_literal};
1515

1616
/// **What it does:** This lint checks for function arguments and let bindings denoted as `ref`. It is `Warn` by default.
1717
///
@@ -345,14 +345,17 @@ impl LintPass for UsedUnderscoreBinding {
345345

346346
impl LateLintPass for UsedUnderscoreBinding {
347347
fn check_expr(&mut self, cx: &LateContext, expr: &Expr) {
348+
if in_attributes_expansion(cx, expr) { // Don't lint things expanded by #[derive(...)], etc
349+
return;
350+
}
348351
let needs_lint = match expr.node {
349352
ExprPath(_, ref path) => {
350353
let ident = path.segments.last()
351354
.expect("path should always have at least one segment")
352355
.identifier;
353356
ident.name.as_str().chars().next() == Some('_') //starts with '_'
354357
&& ident.name.as_str().chars().skip(1).next() != Some('_') //doesn't start with "__"
355-
&& ident.name != ident.unhygienic_name //not in macro
358+
&& ident.name != ident.unhygienic_name //not in bang macro
356359
&& is_used(cx, expr)
357360
},
358361
ExprField(_, spanned) => {
@@ -362,9 +365,6 @@ impl LateLintPass for UsedUnderscoreBinding {
362365
},
363366
_ => false
364367
};
365-
if in_external_macro(cx, expr.span) {
366-
return
367-
}
368368
if needs_lint {
369369
cx.span_lint(USED_UNDERSCORE_BINDING, expr.span,
370370
"used binding which is prefixed with an underscore. A leading underscore \
@@ -373,6 +373,8 @@ impl LateLintPass for UsedUnderscoreBinding {
373373
}
374374
}
375375

376+
/// Heuristic to see if an expression is used. Should be compatible with `unused_variables`'s idea
377+
/// of what it means for an expression to be "used".
376378
fn is_used(cx: &LateContext, expr: &Expr) -> bool {
377379
if let Some(ref parent) = get_parent_expr(cx, expr) {
378380
match parent.node {
@@ -385,3 +387,16 @@ fn is_used(cx: &LateContext, expr: &Expr) -> bool {
385387
true
386388
}
387389
}
390+
391+
/// Test whether an expression is in a macro expansion (e.g. something generated by #[derive(...)]
392+
/// or the like)
393+
fn in_attributes_expansion(cx: &LateContext, expr: &Expr) -> bool {
394+
cx.sess().codemap().with_expn_info(expr.span.expn_id, |info_opt| {
395+
info_opt.map_or(false, |info| {
396+
match info.callee.format {
397+
ExpnFormat::MacroAttribute(_) => true,
398+
_ => false,
399+
}
400+
})
401+
})
402+
}

tests/compile-fail/used_underscore_binding.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ fn prefix_underscore(_foo: u32) -> u32 {
99

1010
/// Test that we lint even if the use is within a macro expansion
1111
fn in_macro(_foo: u32) {
12-
println!("{}", _foo); // doesn't warn, nut should #507
12+
println!("{}", _foo); //~ ERROR used binding which is prefixed with an underscore
1313
}
1414

1515
// Struct for testing use of fields prefixed with an underscore
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#![feature(plugin)]
2+
#![plugin(clippy)]
3+
4+
extern crate rustc_serialize;
5+
6+
/// Test that we do not lint for unused underscores in a MacroAttribute expansion
7+
#[deny(used_underscore_binding)]
8+
#[derive(RustcEncodable)]
9+
struct MacroAttributesTest {
10+
_foo: u32,
11+
}
12+
13+
#[test]
14+
fn macro_attributes_test() {
15+
let _ = MacroAttributesTest{_foo: 0};
16+
}

0 commit comments

Comments
 (0)