Skip to content

Implement #507 #508

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 21, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ compiletest_rs = "0.0.11"
regex = "*"
regex_macros = "*"
lazy_static = "*"
rustc-serialize = "0.3"

[features]

Expand Down
27 changes: 21 additions & 6 deletions src/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ use syntax::ptr::P;
use rustc_front::hir::*;
use reexport::*;
use rustc_front::util::{is_comparison_binop, binop_to_string};
use syntax::codemap::{Span, Spanned};
use syntax::codemap::{Span, Spanned, ExpnFormat};
use rustc_front::intravisit::FnKind;
use rustc::middle::ty;
use rustc::middle::const_eval::ConstVal::Float;
use rustc::middle::const_eval::eval_const_expr_partial;
use rustc::middle::const_eval::EvalHint::ExprTypeChecked;

use utils::{get_item_name, match_path, snippet, get_parent_expr, span_lint};
use utils::{span_help_and_lint, in_external_macro, walk_ptrs_ty, is_integer_literal};
use utils::{span_help_and_lint, walk_ptrs_ty, is_integer_literal};

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

impl LateLintPass for UsedUnderscoreBinding {
fn check_expr(&mut self, cx: &LateContext, expr: &Expr) {
if in_attributes_expansion(cx, expr) { // Don't lint things expanded by #[derive(...)], etc
return;
}
let needs_lint = match expr.node {
ExprPath(_, ref path) => {
let ident = path.segments.last()
.expect("path should always have at least one segment")
.identifier;
ident.name.as_str().chars().next() == Some('_') //starts with '_'
&& ident.name.as_str().chars().skip(1).next() != Some('_') //doesn't start with "__"
&& ident.name != ident.unhygienic_name //not in macro
&& ident.name != ident.unhygienic_name //not in bang macro
&& is_used(cx, expr)
},
ExprField(_, spanned) => {
Expand All @@ -362,9 +365,6 @@ impl LateLintPass for UsedUnderscoreBinding {
},
_ => false
};
if in_external_macro(cx, expr.span) {
return
}
if needs_lint {
cx.span_lint(USED_UNDERSCORE_BINDING, expr.span,
"used binding which is prefixed with an underscore. A leading underscore \
Expand All @@ -373,6 +373,8 @@ impl LateLintPass for UsedUnderscoreBinding {
}
}

/// Heuristic to see if an expression is used. Should be compatible with `unused_variables`'s idea
/// of what it means for an expression to be "used".
fn is_used(cx: &LateContext, expr: &Expr) -> bool {
if let Some(ref parent) = get_parent_expr(cx, expr) {
match parent.node {
Expand All @@ -385,3 +387,16 @@ fn is_used(cx: &LateContext, expr: &Expr) -> bool {
true
}
}

/// Test whether an expression is in a macro expansion (e.g. something generated by #[derive(...)]
/// or the like)
fn in_attributes_expansion(cx: &LateContext, expr: &Expr) -> bool {
cx.sess().codemap().with_expn_info(expr.span.expn_id, |info_opt| {
info_opt.map_or(false, |info| {
match info.callee.format {
ExpnFormat::MacroAttribute(_) => true,
_ => false,
}
})
})
}
2 changes: 1 addition & 1 deletion tests/compile-fail/used_underscore_binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ fn prefix_underscore(_foo: u32) -> u32 {

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

// Struct for testing use of fields prefixed with an underscore
Expand Down
16 changes: 16 additions & 0 deletions tests/used_underscore_binding_macro.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#![feature(plugin)]
#![plugin(clippy)]

extern crate rustc_serialize;

/// Test that we do not lint for unused underscores in a MacroAttribute expansion
#[deny(used_underscore_binding)]
#[derive(RustcEncodable)]
struct MacroAttributesTest {
_foo: u32,
}

#[test]
fn macro_attributes_test() {
let _ = MacroAttributesTest{_foo: 0};
}