additional macro check + more tests

This commit is contained in:
llogiq 2015-09-10 08:51:14 +02:00
parent d82c13d92b
commit 0e1bc74683
3 changed files with 17 additions and 2 deletions

View File

@ -50,12 +50,12 @@ pub struct LetPass;
declare_lint!(pub LET_UNIT_VALUE, Warn,
"creating a let binding to a value of unit type, which usually can't be used afterwards");
fn check_let_unit(cx: &Context, decl: &Decl) {
if let DeclLocal(ref local) = decl.node {
let bindtype = &cx.tcx.pat_ty(&local.pat).sty;
if *bindtype == ty::TyTuple(vec![]) {
if in_external_macro(cx, decl.span) { return; }
if in_external_macro(cx, decl.span) ||
in_macro(cx, local.pat.span) { return; }
span_lint(cx, LET_UNIT_VALUE, decl.span, &format!(
"this let-binding has unit value. Consider omitting `let {} =`",
snippet(cx, local.pat.span, "..")));

View File

@ -2,6 +2,13 @@
#![plugin(clippy)]
#![deny(let_unit_value)]
#![allow(unused_variables)]
macro_rules! let_and_return {
($n:expr) => {{
let ret = $n;
}}
}
fn main() {
let _x = println!("x"); //~ERROR this let-binding has unit value
@ -10,4 +17,9 @@ fn main() {
if true {
let _a = (); //~ERROR this let-binding has unit value
}
let_and_return!(()) // should be fine
}
#[derive(Copy, Clone)]
pub struct ContainsUnit(()); // should be fine

View File

@ -3,6 +3,9 @@
#![deny(unit_cmp)]
#[derive(PartialEq)]
pub struct ContainsUnit(()); // should be fine
fn main() {
// this is fine
if true == false {