1 use rustc_ast::ast::Attribute;
2 use rustc_errors::Applicability;
3 use rustc_hir::def_id::{DefIdSet, LocalDefId};
4 use rustc_hir::{self as hir, def::Res, intravisit, QPath};
5 use rustc_lint::{LateContext, LintContext};
8 lint::in_external_macro,
11 use rustc_span::{sym, Span};
13 use clippy_utils::attrs::is_proc_macro;
14 use clippy_utils::diagnostics::{span_lint_and_help, span_lint_and_then};
15 use clippy_utils::source::snippet_opt;
16 use clippy_utils::ty::is_must_use_ty;
17 use clippy_utils::{match_def_path, must_use_attr, return_ty, trait_ref_of_method};
19 use super::{DOUBLE_MUST_USE, MUST_USE_CANDIDATE, MUST_USE_UNIT};
21 pub(super) fn check_item(cx: &LateContext<'tcx>, item: &'tcx hir::Item<'_>) {
22 let attrs = cx.tcx.hir().attrs(item.hir_id());
23 let attr = must_use_attr(attrs);
24 if let hir::ItemKind::Fn(ref sig, ref _generics, ref body_id) = item.kind {
25 let is_public = cx.access_levels.is_exported(item.def_id);
26 let fn_header_span = item.span.with_hi(sig.decl.output.span().hi());
27 if let Some(attr) = attr {
28 check_needless_must_use(cx, sig.decl, item.hir_id(), item.span, fn_header_span, attr);
29 } else if is_public && !is_proc_macro(cx.sess(), attrs) && !attrs.iter().any(|a| a.has_name(sym::no_mangle)) {
30 check_must_use_candidate(
33 cx.tcx.hir().body(*body_id),
36 item.span.with_hi(sig.decl.output.span().hi()),
37 "this function could have a `#[must_use]` attribute",
43 pub(super) fn check_impl_item(cx: &LateContext<'tcx>, item: &'tcx hir::ImplItem<'_>) {
44 if let hir::ImplItemKind::Fn(ref sig, ref body_id) = item.kind {
45 let is_public = cx.access_levels.is_exported(item.def_id);
46 let fn_header_span = item.span.with_hi(sig.decl.output.span().hi());
47 let attrs = cx.tcx.hir().attrs(item.hir_id());
48 let attr = must_use_attr(attrs);
49 if let Some(attr) = attr {
50 check_needless_must_use(cx, sig.decl, item.hir_id(), item.span, fn_header_span, attr);
51 } else if is_public && !is_proc_macro(cx.sess(), attrs) && trait_ref_of_method(cx, item.hir_id()).is_none() {
52 check_must_use_candidate(
55 cx.tcx.hir().body(*body_id),
58 item.span.with_hi(sig.decl.output.span().hi()),
59 "this method could have a `#[must_use]` attribute",
65 pub(super) fn check_trait_item(cx: &LateContext<'tcx>, item: &'tcx hir::TraitItem<'_>) {
66 if let hir::TraitItemKind::Fn(ref sig, ref eid) = item.kind {
67 let is_public = cx.access_levels.is_exported(item.def_id);
68 let fn_header_span = item.span.with_hi(sig.decl.output.span().hi());
70 let attrs = cx.tcx.hir().attrs(item.hir_id());
71 let attr = must_use_attr(attrs);
72 if let Some(attr) = attr {
73 check_needless_must_use(cx, sig.decl, item.hir_id(), item.span, fn_header_span, attr);
74 } else if let hir::TraitFn::Provided(eid) = *eid {
75 let body = cx.tcx.hir().body(eid);
76 if attr.is_none() && is_public && !is_proc_macro(cx.sess(), attrs) {
77 check_must_use_candidate(
83 item.span.with_hi(sig.decl.output.span().hi()),
84 "this method could have a `#[must_use]` attribute",
91 fn check_needless_must_use(
93 decl: &hir::FnDecl<'_>,
99 if in_external_macro(cx.sess(), item_span) {
102 if returns_unit(decl) {
107 "this unit-returning function has a `#[must_use]` attribute",
109 diag.span_suggestion(
111 "remove the attribute",
113 Applicability::MachineApplicable,
117 } else if attr.value_str().is_none() && is_must_use_ty(cx, return_ty(cx, item_id)) {
122 "this function has an empty `#[must_use]` attribute, but returns a type already marked as `#[must_use]`",
124 "either add some descriptive text or remove the attribute",
129 fn check_must_use_candidate<'tcx>(
130 cx: &LateContext<'tcx>,
131 decl: &'tcx hir::FnDecl<'_>,
132 body: &'tcx hir::Body<'_>,
138 if has_mutable_arg(cx, body)
139 || mutates_static(cx, body)
140 || in_external_macro(cx.sess(), item_span)
141 || returns_unit(decl)
142 || !cx.access_levels.is_exported(item_id)
143 || is_must_use_ty(cx, return_ty(cx, cx.tcx.hir().local_def_id_to_hir_id(item_id)))
147 span_lint_and_then(cx, MUST_USE_CANDIDATE, fn_span, msg, |diag| {
148 if let Some(snippet) = snippet_opt(cx, fn_span) {
149 diag.span_suggestion(
152 format!("#[must_use] {}", snippet),
153 Applicability::MachineApplicable,
159 fn returns_unit(decl: &hir::FnDecl<'_>) -> bool {
161 hir::FnRetTy::DefaultReturn(_) => true,
162 hir::FnRetTy::Return(ty) => match ty.kind {
163 hir::TyKind::Tup(tys) => tys.is_empty(),
164 hir::TyKind::Never => true,
170 fn has_mutable_arg(cx: &LateContext<'_>, body: &hir::Body<'_>) -> bool {
171 let mut tys = DefIdSet::default();
172 body.params.iter().any(|param| is_mutable_pat(cx, param.pat, &mut tys))
175 fn is_mutable_pat(cx: &LateContext<'_>, pat: &hir::Pat<'_>, tys: &mut DefIdSet) -> bool {
176 if let hir::PatKind::Wild = pat.kind {
177 return false; // ignore `_` patterns
179 if cx.tcx.has_typeck_results(pat.hir_id.owner.to_def_id()) {
180 is_mutable_ty(cx, cx.tcx.typeck(pat.hir_id.owner).pat_ty(pat), pat.span, tys)
186 static KNOWN_WRAPPER_TYS: &[&[&str]] = &[&["alloc", "rc", "Rc"], &["std", "sync", "Arc"]];
188 fn is_mutable_ty<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>, span: Span, tys: &mut DefIdSet) -> bool {
190 // primitive types are never mutable
191 ty::Bool | ty::Char | ty::Int(_) | ty::Uint(_) | ty::Float(_) | ty::Str => false,
192 ty::Adt(adt, substs) => {
193 tys.insert(adt.did) && !ty.is_freeze(cx.tcx.at(span), cx.param_env)
194 || KNOWN_WRAPPER_TYS.iter().any(|path| match_def_path(cx, adt.did, path))
195 && substs.types().any(|ty| is_mutable_ty(cx, ty, span, tys))
197 ty::Tuple(substs) => substs.types().any(|ty| is_mutable_ty(cx, ty, span, tys)),
198 ty::Array(ty, _) | ty::Slice(ty) => is_mutable_ty(cx, ty, span, tys),
199 ty::RawPtr(ty::TypeAndMut { ty, mutbl }) | ty::Ref(_, ty, mutbl) => {
200 mutbl == hir::Mutability::Mut || is_mutable_ty(cx, ty, span, tys)
202 // calling something constitutes a side effect, so return true on all callables
203 // also never calls need not be used, so return true for them, too
208 struct StaticMutVisitor<'a, 'tcx> {
209 cx: &'a LateContext<'tcx>,
210 mutates_static: bool,
213 impl<'a, 'tcx> intravisit::Visitor<'tcx> for StaticMutVisitor<'a, 'tcx> {
214 type Map = Map<'tcx>;
216 fn visit_expr(&mut self, expr: &'tcx hir::Expr<'_>) {
217 use hir::ExprKind::{AddrOf, Assign, AssignOp, Call, MethodCall};
219 if self.mutates_static {
223 Call(_, args) | MethodCall(_, _, args, _) => {
224 let mut tys = DefIdSet::default();
226 if self.cx.tcx.has_typeck_results(arg.hir_id.owner.to_def_id())
229 self.cx.tcx.typeck(arg.hir_id.owner).expr_ty(arg),
233 && is_mutated_static(arg)
235 self.mutates_static = true;
241 Assign(target, ..) | AssignOp(_, target, _) | AddrOf(_, hir::Mutability::Mut, target) => {
242 self.mutates_static |= is_mutated_static(target);
248 fn nested_visit_map(&mut self) -> intravisit::NestedVisitorMap<Self::Map> {
249 intravisit::NestedVisitorMap::None
253 fn is_mutated_static(e: &hir::Expr<'_>) -> bool {
254 use hir::ExprKind::{Field, Index, Path};
257 Path(QPath::Resolved(_, path)) => !matches!(path.res, Res::Local(_)),
259 Field(inner, _) | Index(inner, _) => is_mutated_static(inner),
264 fn mutates_static<'tcx>(cx: &LateContext<'tcx>, body: &'tcx hir::Body<'_>) -> bool {
265 let mut v = StaticMutVisitor {
267 mutates_static: false,
269 intravisit::walk_expr(&mut v, &body.value);