Merge pull request #1959 from DarkEld3r/1884-borrowed-box-any

Fix borrowed_box lint for Box<Any>
This commit is contained in:
Oliver Schneider 2017-08-24 23:59:15 +02:00 committed by GitHub
commit ab362031ed
3 changed files with 72 additions and 1 deletions

View File

@ -10,7 +10,7 @@ use syntax::ast::{IntTy, UintTy, FloatTy};
use syntax::attr::IntType;
use syntax::codemap::Span;
use utils::{comparisons, higher, in_external_macro, in_macro, match_def_path, snippet, span_help_and_lint, span_lint,
span_lint_and_sugg, opt_def_id, last_path_segment, type_size};
span_lint_and_sugg, opt_def_id, last_path_segment, type_size, match_path_old};
use utils::paths;
/// Handles all the linting of funky types
@ -212,6 +212,11 @@ fn check_ty(cx: &LateContext, ast_ty: &hir::Ty, is_local: bool) {
let PathParameters::AngleBracketedParameters(ref ab_data) = bx.parameters,
let [ref inner] = *ab_data.types
], {
if is_any_trait(inner) {
// Ignore `Box<Any>` types, see #1884 for details.
return;
}
let ltopt = if lt.is_elided() {
"".to_owned()
} else {
@ -249,6 +254,21 @@ fn check_ty(cx: &LateContext, ast_ty: &hir::Ty, is_local: bool) {
}
}
// Returns true if given type is `Any` trait.
fn is_any_trait(t: &hir::Ty) -> bool {
if_let_chain! {[
let TyTraitObject(ref traits, _) = t.node,
traits.len() >= 1,
// Only Send/Sync can be used as additional traits, so it is enough to
// check only the first trait.
match_path_old(&traits[0].trait_ref.path, &paths::ANY_TRAIT)
], {
return true;
}}
false
}
#[allow(missing_copy_implementations)]
pub struct LetPass;

View File

@ -1,6 +1,7 @@
//! This module contains paths to types and functions Clippy needs to know
//! about.
pub const ANY_TRAIT: [&'static str; 3] = ["std", "any", "Any"];
pub const ASMUT_TRAIT: [&'static str; 3] = ["core", "convert", "AsMut"];
pub const ASREF_TRAIT: [&'static str; 3] = ["core", "convert", "AsRef"];
pub const BEGIN_PANIC: [&'static str; 3] = ["std", "panicking", "begin_panic"];

View File

@ -28,7 +28,57 @@ impl<'a> Test4 for Test3<'a> {
}
}
use std::any::Any;
pub fn test5(foo: &mut Box<Any>) {
println!("{:?}", foo)
}
pub fn test6() {
let foo: &Box<Any>;
}
struct Test7<'a> {
foo: &'a Box<Any>
}
trait Test8 {
fn test8(a: &Box<Any>);
}
impl<'a> Test8 for Test7<'a> {
fn test8(a: &Box<Any>) {
unimplemented!();
}
}
pub fn test9(foo: &mut Box<Any + Send + Sync>) {
let _ = foo;
}
pub fn test10() {
let foo: &Box<Any + Send + 'static>;
}
struct Test11<'a> {
foo: &'a Box<Any + Send>
}
trait Test12 {
fn test4(a: &Box<Any + 'static>);
}
impl<'a> Test12 for Test11<'a> {
fn test4(a: &Box<Any + 'static>) {
unimplemented!();
}
}
fn main(){
test1(&mut Box::new(false));
test2();
test5(&mut (Box::new(false) as Box<Any>));
test6();
test9(&mut (Box::new(false) as Box<Any + Send + Sync>));
test10();
}