fix no_effect lint

This commit is contained in:
Oliver Schneider 2016-05-25 18:51:35 +02:00
parent 77ed899941
commit bb69e60b30
No known key found for this signature in database
GPG key ID: 56D6EEA0FC67AC46
2 changed files with 11 additions and 5 deletions

View file

@ -1,6 +1,6 @@
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass}; use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
use rustc::hir::def::{Def, PathResolution}; use rustc::hir::def::{Def, PathResolution};
use rustc::hir::{Expr, Expr_, Stmt, StmtSemi}; use rustc::hir::{Expr, Expr_, Stmt, StmtSemi, BlockCheckMode, UnsafeSource};
use utils::{in_macro, span_lint, snippet_opt, span_lint_and_then}; use utils::{in_macro, span_lint, snippet_opt, span_lint_and_then};
use std::ops::Deref; use std::ops::Deref;
@ -140,11 +140,11 @@ fn reduce_expression<'a>(cx: &LateContext, expr: &'a Expr) -> Option<Vec<&'a Exp
} }
Expr_::ExprBlock(ref block) => { Expr_::ExprBlock(ref block) => {
if block.stmts.is_empty() { if block.stmts.is_empty() {
block.expr.as_ref().and_then(|e| if e.span == expr.span { block.expr.as_ref().and_then(|e| match block.rules {
BlockCheckMode::UnsafeBlock(UnsafeSource::UserProvided) => None,
BlockCheckMode::DefaultBlock => Some(vec![&**e]),
// in case of compiler-inserted signaling blocks // in case of compiler-inserted signaling blocks
reduce_expression(cx, e) _ => reduce_expression(cx, e),
} else {
Some(vec![e])
}) })
} else { } else {
None None

View file

@ -18,6 +18,8 @@ enum Enum {
fn get_number() -> i32 { 0 } fn get_number() -> i32 { 0 }
fn get_struct() -> Struct { Struct { field: 0 } } fn get_struct() -> Struct { Struct { field: 0 } }
unsafe fn unsafe_fn() -> i32 { 0 }
fn main() { fn main() {
let s = get_struct(); let s = get_struct();
let s2 = get_struct(); let s2 = get_struct();
@ -50,6 +52,7 @@ fn main() {
// Do not warn // Do not warn
get_number(); get_number();
unsafe { unsafe_fn() };
Tuple(get_number()); //~ERROR statement can be reduced Tuple(get_number()); //~ERROR statement can be reduced
//~^HELP replace it with //~^HELP replace it with
@ -105,4 +108,7 @@ fn main() {
[42; 55][get_number() as usize]; //~ERROR statement can be reduced [42; 55][get_number() as usize]; //~ERROR statement can be reduced
//~^HELP replace it with //~^HELP replace it with
//~|SUGGESTION [42; 55];get_number() as usize; //~|SUGGESTION [42; 55];get_number() as usize;
{get_number()}; //~ERROR statement can be reduced
//~^HELP replace it with
//~|SUGGESTION get_number();
} }