Skip to content

Commit cd97651

Browse files
basil-cowareredify
authored and
areredify
committed
move is_must_use_ty to utils
1 parent d82debb commit cd97651

File tree

2 files changed

+48
-45
lines changed

2 files changed

+48
-45
lines changed

clippy_lints/src/functions.rs

+1-44
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::utils::{
22
attrs::is_proc_macro, iter_input_pats, match_def_path, qpath_res, return_ty, snippet, snippet_opt,
33
span_help_and_lint, span_lint, span_lint_and_then, trait_ref_of_method, type_is_unsafe_function,
4+
must_use_attr, is_must_use_ty,
45
};
56
use matches::matches;
67
use rustc::hir::{self, def::Res, def_id::DefId, intravisit};
@@ -466,15 +467,6 @@ fn check_must_use_candidate<'a, 'tcx>(
466467
});
467468
}
468469

469-
fn must_use_attr(attrs: &[Attribute]) -> Option<&Attribute> {
470-
attrs.iter().find(|attr| {
471-
attr.ident().map_or(false, |ident| {
472-
let ident: &str = &ident.as_str();
473-
"must_use" == ident
474-
})
475-
})
476-
}
477-
478470
fn returns_unit(decl: &hir::FnDecl) -> bool {
479471
match decl.output {
480472
hir::FunctionRetTy::DefaultReturn(_) => true,
@@ -486,41 +478,6 @@ fn returns_unit(decl: &hir::FnDecl) -> bool {
486478
}
487479
}
488480

489-
fn is_must_use_ty<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, ty: Ty<'tcx>) -> bool {
490-
use ty::TyKind::*;
491-
match ty.kind {
492-
Adt(ref adt, _) => must_use_attr(&cx.tcx.get_attrs(adt.did)).is_some(),
493-
Foreign(ref did) => must_use_attr(&cx.tcx.get_attrs(*did)).is_some(),
494-
Slice(ref ty) | Array(ref ty, _) | RawPtr(ty::TypeAndMut { ref ty, .. }) | Ref(_, ref ty, _) => {
495-
// for the Array case we don't need to care for the len == 0 case
496-
// because we don't want to lint functions returning empty arrays
497-
is_must_use_ty(cx, *ty)
498-
},
499-
Tuple(ref substs) => substs.types().any(|ty| is_must_use_ty(cx, ty)),
500-
Opaque(ref def_id, _) => {
501-
for (predicate, _) in cx.tcx.predicates_of(*def_id).predicates {
502-
if let ty::Predicate::Trait(ref poly_trait_predicate) = predicate {
503-
if must_use_attr(&cx.tcx.get_attrs(poly_trait_predicate.skip_binder().trait_ref.def_id)).is_some() {
504-
return true;
505-
}
506-
}
507-
}
508-
false
509-
},
510-
Dynamic(binder, _) => {
511-
for predicate in binder.skip_binder().iter() {
512-
if let ty::ExistentialPredicate::Trait(ref trait_ref) = predicate {
513-
if must_use_attr(&cx.tcx.get_attrs(trait_ref.def_id)).is_some() {
514-
return true;
515-
}
516-
}
517-
}
518-
false
519-
},
520-
_ => false,
521-
}
522-
}
523-
524481
fn has_mutable_arg(cx: &LateContext<'_, '_>, body: &hir::Body) -> bool {
525482
let mut tys = FxHashSet::default();
526483
body.params.iter().any(|param| is_mutable_pat(cx, &param.pat, &mut tys))

clippy_lints/src/utils/mod.rs

+47-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ use rustc::ty::{
4141
};
4242
use rustc_errors::Applicability;
4343
use smallvec::SmallVec;
44-
use syntax::ast::{self, LitKind};
44+
use syntax::ast::{self, LitKind, Attribute};
4545
use syntax::attr;
4646
use syntax::source_map::{Span, DUMMY_SP};
4747
use syntax::symbol::{kw, Symbol};
@@ -1237,3 +1237,49 @@ pub fn parent_node_is_if_expr<'a, 'b>(expr: &Expr, cx: &LateContext<'a, 'b>) ->
12371237
_ => false,
12381238
}
12391239
}
1240+
1241+
pub fn must_use_attr(attrs: &[Attribute]) -> Option<&Attribute> {
1242+
attrs.iter().find(|attr| {
1243+
attr.ident().map_or(false, |ident| {
1244+
let ident: &str = &ident.as_str();
1245+
"must_use" == ident
1246+
})
1247+
})
1248+
}
1249+
1250+
// Returns whether the type has #[must_use] attribute
1251+
pub fn is_must_use_ty<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, ty: Ty<'tcx>) -> bool {
1252+
use ty::TyKind::*;
1253+
match ty.kind {
1254+
Adt(ref adt, _) => must_use_attr(&cx.tcx.get_attrs(adt.did)).is_some(),
1255+
Foreign(ref did) => must_use_attr(&cx.tcx.get_attrs(*did)).is_some(),
1256+
Slice(ref ty) | Array(ref ty, _) | RawPtr(ty::TypeAndMut { ref ty, .. }) | Ref(_, ref ty, _) => {
1257+
// for the Array case we don't need to care for the len == 0 case
1258+
// because we don't want to lint functions returning empty arrays
1259+
is_must_use_ty(cx, *ty)
1260+
},
1261+
Tuple(ref substs) => substs.types().any(|ty| is_must_use_ty(cx, ty)),
1262+
Opaque(ref def_id, _) => {
1263+
for (predicate, _) in cx.tcx.predicates_of(*def_id).predicates {
1264+
if let ty::Predicate::Trait(ref poly_trait_predicate) = predicate {
1265+
if must_use_attr(&cx.tcx.get_attrs(poly_trait_predicate.skip_binder().trait_ref.def_id)).is_some() {
1266+
return true;
1267+
}
1268+
}
1269+
}
1270+
false
1271+
},
1272+
Dynamic(binder, _) => {
1273+
for predicate in binder.skip_binder().iter() {
1274+
if let ty::ExistentialPredicate::Trait(ref trait_ref) = predicate {
1275+
if must_use_attr(&cx.tcx.get_attrs(trait_ref.def_id)).is_some() {
1276+
return true;
1277+
}
1278+
}
1279+
}
1280+
false
1281+
},
1282+
_ => false,
1283+
}
1284+
}
1285+

0 commit comments

Comments
 (0)