From 0e1bc74683c7901f8432499eccb890f9f6b29ad9 Mon Sep 17 00:00:00 2001 From: llogiq Date: Thu, 10 Sep 2015 08:51:14 +0200 Subject: [PATCH] additional macro check + more tests --- src/types.rs | 4 ++-- tests/compile-fail/let_unit.rs | 12 ++++++++++++ tests/compile-fail/unit_cmp.rs | 3 +++ 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/types.rs b/src/types.rs index 6d096e18b88..08a8560fcd6 100644 --- a/src/types.rs +++ b/src/types.rs @@ -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, ".."))); diff --git a/tests/compile-fail/let_unit.rs b/tests/compile-fail/let_unit.rs index f06a10bfe13..a0143406e52 100755 --- a/tests/compile-fail/let_unit.rs +++ b/tests/compile-fail/let_unit.rs @@ -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 diff --git a/tests/compile-fail/unit_cmp.rs b/tests/compile-fail/unit_cmp.rs index e246d9f3909..af28f849e8c 100755 --- a/tests/compile-fail/unit_cmp.rs +++ b/tests/compile-fail/unit_cmp.rs @@ -3,6 +3,9 @@ #![deny(unit_cmp)] +#[derive(PartialEq)] +pub struct ContainsUnit(()); // should be fine + fn main() { // this is fine if true == false {