Implement #507
Make `used_underscore_binding` lint compatible with MacroAttributes expansions. TODO: Add a good test for this.
This commit is contained in:
parent
4a32445aa7
commit
b190aa7deb
27
src/misc.rs
27
src/misc.rs
@ -3,7 +3,7 @@
|
|||||||
use rustc_front::hir::*;
|
use rustc_front::hir::*;
|
||||||
use reexport::*;
|
use reexport::*;
|
||||||
use rustc_front::util::{is_comparison_binop, binop_to_string};
|
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_front::intravisit::FnKind;
|
||||||
use rustc::middle::ty;
|
use rustc::middle::ty;
|
||||||
use rustc::middle::const_eval::ConstVal::Float;
|
use rustc::middle::const_eval::ConstVal::Float;
|
||||||
@ -11,7 +11,7 @@
|
|||||||
use rustc::middle::const_eval::EvalHint::ExprTypeChecked;
|
use rustc::middle::const_eval::EvalHint::ExprTypeChecked;
|
||||||
|
|
||||||
use utils::{get_item_name, match_path, snippet, get_parent_expr, span_lint};
|
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.
|
/// **What it does:** This lint checks for function arguments and let bindings denoted as `ref`. It is `Warn` by default.
|
||||||
///
|
///
|
||||||
@ -345,6 +345,9 @@ fn get_lints(&self) -> LintArray {
|
|||||||
|
|
||||||
impl LateLintPass for UsedUnderscoreBinding {
|
impl LateLintPass for UsedUnderscoreBinding {
|
||||||
fn check_expr(&mut self, cx: &LateContext, expr: &Expr) {
|
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 {
|
let needs_lint = match expr.node {
|
||||||
ExprPath(_, ref path) => {
|
ExprPath(_, ref path) => {
|
||||||
let ident = path.segments.last()
|
let ident = path.segments.last()
|
||||||
@ -352,7 +355,7 @@ fn check_expr(&mut self, cx: &LateContext, expr: &Expr) {
|
|||||||
.identifier;
|
.identifier;
|
||||||
ident.name.as_str().chars().next() == Some('_') //starts with '_'
|
ident.name.as_str().chars().next() == Some('_') //starts with '_'
|
||||||
&& ident.name.as_str().chars().skip(1).next() != Some('_') //doesn't start 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)
|
&& is_used(cx, expr)
|
||||||
},
|
},
|
||||||
ExprField(_, spanned) => {
|
ExprField(_, spanned) => {
|
||||||
@ -362,9 +365,6 @@ fn check_expr(&mut self, cx: &LateContext, expr: &Expr) {
|
|||||||
},
|
},
|
||||||
_ => false
|
_ => false
|
||||||
};
|
};
|
||||||
if in_external_macro(cx, expr.span) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if needs_lint {
|
if needs_lint {
|
||||||
cx.span_lint(USED_UNDERSCORE_BINDING, expr.span,
|
cx.span_lint(USED_UNDERSCORE_BINDING, expr.span,
|
||||||
"used binding which is prefixed with an underscore. A leading underscore \
|
"used binding which is prefixed with an underscore. A leading underscore \
|
||||||
@ -373,6 +373,8 @@ fn check_expr(&mut self, cx: &LateContext, expr: &Expr) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// 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 {
|
fn is_used(cx: &LateContext, expr: &Expr) -> bool {
|
||||||
if let Some(ref parent) = get_parent_expr(cx, expr) {
|
if let Some(ref parent) = get_parent_expr(cx, expr) {
|
||||||
match parent.node {
|
match parent.node {
|
||||||
@ -385,3 +387,16 @@ fn is_used(cx: &LateContext, expr: &Expr) -> bool {
|
|||||||
true
|
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,
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
@ -9,7 +9,15 @@ fn prefix_underscore(_foo: u32) -> u32 {
|
|||||||
|
|
||||||
/// Test that we lint even if the use is within a macro expansion
|
/// Test that we lint even if the use is within a macro expansion
|
||||||
fn in_macro(_foo: u32) {
|
fn in_macro(_foo: u32) {
|
||||||
println!("{}", _foo); // doesn't warn, nut should #507
|
println!("{}", _foo); //~ ERROR used binding which is prefixed with an underscore
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: This doesn't actually correctly test this. Need to find a #[derive(...)] which sets off
|
||||||
|
// the lint if the `in_attributes_expansion` test isn't there
|
||||||
|
/// Test that we do not lint for unused underscores in a MacroAttribute expansion
|
||||||
|
#[derive(Clone)]
|
||||||
|
struct MacroAttributesTest {
|
||||||
|
_foo: u32,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Struct for testing use of fields prefixed with an underscore
|
// Struct for testing use of fields prefixed with an underscore
|
||||||
@ -68,6 +76,7 @@ fn non_variables() {
|
|||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let foo = 0u32;
|
let foo = 0u32;
|
||||||
|
let _ = MacroAttributesTest{_foo: 0};
|
||||||
// tests of unused_underscore lint
|
// tests of unused_underscore lint
|
||||||
let _ = prefix_underscore(foo);
|
let _ = prefix_underscore(foo);
|
||||||
in_macro(foo);
|
in_macro(foo);
|
||||||
|
Loading…
Reference in New Issue
Block a user