Skip to content

Commit

Permalink
move is_must_use_ty to utils
Browse files Browse the repository at this point in the history
  • Loading branch information
basil-cow authored and Areredify committed Dec 7, 2019
1 parent 61b19a1 commit 68ab305
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 45 deletions.
45 changes: 1 addition & 44 deletions clippy_lints/src/functions.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::utils::{
attrs::is_proc_macro, iter_input_pats, match_def_path, qpath_res, return_ty, snippet, snippet_opt,
span_help_and_lint, span_lint, span_lint_and_then, trait_ref_of_method, type_is_unsafe_function,
must_use_attr, is_must_use_ty,
};
use matches::matches;
use rustc::hir::{self, def::Res, def_id::DefId, intravisit};
Expand Down Expand Up @@ -465,15 +466,6 @@ fn check_must_use_candidate<'a, 'tcx>(
});
}

fn must_use_attr(attrs: &[Attribute]) -> Option<&Attribute> {
attrs.iter().find(|attr| {
attr.ident().map_or(false, |ident| {
let ident: &str = &ident.as_str();
"must_use" == ident
})
})
}

fn returns_unit(decl: &hir::FnDecl) -> bool {
match decl.output {
hir::FunctionRetTy::DefaultReturn(_) => true,
Expand All @@ -485,41 +477,6 @@ fn returns_unit(decl: &hir::FnDecl) -> bool {
}
}

fn is_must_use_ty<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, ty: Ty<'tcx>) -> bool {
use ty::TyKind::*;
match ty.kind {
Adt(ref adt, _) => must_use_attr(&cx.tcx.get_attrs(adt.did)).is_some(),
Foreign(ref did) => must_use_attr(&cx.tcx.get_attrs(*did)).is_some(),
Slice(ref ty) | Array(ref ty, _) | RawPtr(ty::TypeAndMut { ref ty, .. }) | Ref(_, ref ty, _) => {
// for the Array case we don't need to care for the len == 0 case
// because we don't want to lint functions returning empty arrays
is_must_use_ty(cx, *ty)
},
Tuple(ref substs) => substs.types().any(|ty| is_must_use_ty(cx, ty)),
Opaque(ref def_id, _) => {
for (predicate, _) in cx.tcx.predicates_of(*def_id).predicates {
if let ty::Predicate::Trait(ref poly_trait_predicate) = predicate {
if must_use_attr(&cx.tcx.get_attrs(poly_trait_predicate.skip_binder().trait_ref.def_id)).is_some() {
return true;
}
}
}
false
},
Dynamic(binder, _) => {
for predicate in binder.skip_binder().iter() {
if let ty::ExistentialPredicate::Trait(ref trait_ref) = predicate {
if must_use_attr(&cx.tcx.get_attrs(trait_ref.def_id)).is_some() {
return true;
}
}
}
false
},
_ => false,
}
}

fn has_mutable_arg(cx: &LateContext<'_, '_>, body: &hir::Body) -> bool {
let mut tys = FxHashSet::default();
body.params.iter().any(|param| is_mutable_pat(cx, &param.pat, &mut tys))
Expand Down
48 changes: 47 additions & 1 deletion clippy_lints/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ use rustc::ty::{
};
use rustc_errors::Applicability;
use smallvec::SmallVec;
use syntax::ast::{self, LitKind};
use syntax::ast::{self, LitKind, Attribute};
use syntax::attr;
use syntax::source_map::{Span, DUMMY_SP};
use syntax::symbol::{kw, Symbol};
Expand Down Expand Up @@ -1237,3 +1237,49 @@ pub fn parent_node_is_if_expr<'a, 'b>(expr: &Expr, cx: &LateContext<'a, 'b>) ->
_ => false,
}
}

pub fn must_use_attr(attrs: &[Attribute]) -> Option<&Attribute> {
attrs.iter().find(|attr| {
attr.ident().map_or(false, |ident| {
let ident: &str = &ident.as_str();
"must_use" == ident
})
})
}

// Returns whether the type has #[must_use] attribute
pub fn is_must_use_ty<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, ty: Ty<'tcx>) -> bool {
use ty::TyKind::*;
match ty.kind {
Adt(ref adt, _) => must_use_attr(&cx.tcx.get_attrs(adt.did)).is_some(),
Foreign(ref did) => must_use_attr(&cx.tcx.get_attrs(*did)).is_some(),
Slice(ref ty) | Array(ref ty, _) | RawPtr(ty::TypeAndMut { ref ty, .. }) | Ref(_, ref ty, _) => {
// for the Array case we don't need to care for the len == 0 case
// because we don't want to lint functions returning empty arrays
is_must_use_ty(cx, *ty)
},
Tuple(ref substs) => substs.types().any(|ty| is_must_use_ty(cx, ty)),
Opaque(ref def_id, _) => {
for (predicate, _) in cx.tcx.predicates_of(*def_id).predicates {
if let ty::Predicate::Trait(ref poly_trait_predicate) = predicate {
if must_use_attr(&cx.tcx.get_attrs(poly_trait_predicate.skip_binder().trait_ref.def_id)).is_some() {
return true;
}
}
}
false
},
Dynamic(binder, _) => {
for predicate in binder.skip_binder().iter() {
if let ty::ExistentialPredicate::Trait(ref trait_ref) = predicate {
if must_use_attr(&cx.tcx.get_attrs(trait_ref.def_id)).is_some() {
return true;
}
}
}
false
},
_ => false,
}
}

0 comments on commit 68ab305

Please sign in to comment.