2 attr_by_name, attrs::is_proc_macro, is_must_use_ty, iter_input_pats, match_def_path, must_use_attr, qpath_res,
3 return_ty, snippet, snippet_opt, span_help_and_lint, span_lint, span_lint_and_then, trait_ref_of_method,
4 type_is_unsafe_function,
7 use rustc::hir::{self, def::Res, def_id::DefId, intravisit};
8 use rustc::impl_lint_pass;
9 use rustc::lint::{in_external_macro, LateContext, LateLintPass, LintArray, LintContext, LintPass};
10 use rustc::ty::{self, Ty};
11 use rustc_data_structures::fx::FxHashSet;
12 use rustc_errors::Applicability;
13 use rustc_session::declare_tool_lint;
14 use rustc_target::spec::abi::Abi;
15 use syntax::ast::Attribute;
16 use syntax::source_map::Span;
18 declare_clippy_lint! {
19 /// **What it does:** Checks for functions with too many parameters.
21 /// **Why is this bad?** Functions with lots of parameters are considered bad
22 /// style and reduce readability (“what does the 5th parameter mean?”). Consider
23 /// grouping some parameters into a new type.
25 /// **Known problems:** None.
30 /// fn foo(x: u32, y: u32, name: &str, c: Color, w: f32, h: f32, a: f32, b: f32) {
34 pub TOO_MANY_ARGUMENTS,
36 "functions with too many arguments"
39 declare_clippy_lint! {
40 /// **What it does:** Checks for functions with a large amount of lines.
42 /// **Why is this bad?** Functions with a lot of lines are harder to understand
43 /// due to having to look at a larger amount of code to understand what the
44 /// function is doing. Consider splitting the body of the function into
45 /// multiple functions.
47 /// **Known problems:** None.
51 /// fn im_too_long() {
53 /// // ... 100 more LoC
59 "functions with too many lines"
62 declare_clippy_lint! {
63 /// **What it does:** Checks for public functions that dereference raw pointer
64 /// arguments but are not marked unsafe.
66 /// **Why is this bad?** The function should probably be marked `unsafe`, since
67 /// for an arbitrary raw pointer, there is no way of telling for sure if it is
70 /// **Known problems:**
72 /// * It does not check functions recursively so if the pointer is passed to a
73 /// private non-`unsafe` function which does the dereferencing, the lint won't
75 /// * It only checks for arguments whose type are raw pointers, not raw pointers
76 /// got from an argument in some other way (`fn foo(bar: &[*const u8])` or
77 /// `some_argument.get_raw_ptr()`).
81 /// pub fn foo(x: *const u8) {
82 /// println!("{}", unsafe { *x });
85 pub NOT_UNSAFE_PTR_ARG_DEREF,
87 "public functions dereferencing raw pointer arguments but not marked `unsafe`"
90 declare_clippy_lint! {
91 /// **What it does:** Checks for a [`#[must_use]`] attribute on
92 /// unit-returning functions and methods.
94 /// [`#[must_use]`]: https://doc.rust-lang.org/reference/attributes/diagnostics.html#the-must_use-attribute
96 /// **Why is this bad?** Unit values are useless. The attribute is likely
97 /// a remnant of a refactoring that removed the return type.
99 /// **Known problems:** None.
108 "`#[must_use]` attribute on a unit-returning function / method"
111 declare_clippy_lint! {
112 /// **What it does:** Checks for a [`#[must_use]`] attribute without
113 /// further information on functions and methods that return a type already
114 /// marked as `#[must_use]`.
116 /// [`#[must_use]`]: https://doc.rust-lang.org/reference/attributes/diagnostics.html#the-must_use-attribute
118 /// **Why is this bad?** The attribute isn't needed. Not using the result
119 /// will already be reported. Alternatively, one can add some text to the
120 /// attribute to improve the lint message.
122 /// **Known problems:** None.
127 /// fn double_must_use() -> Result<(), ()> {
128 /// unimplemented!();
133 "`#[must_use]` attribute on a `#[must_use]`-returning function / method"
136 declare_clippy_lint! {
137 /// **What it does:** Checks for public functions that have no
138 /// [`#[must_use]`] attribute, but return something not already marked
139 /// must-use, have no mutable arg and mutate no statics.
141 /// [`#[must_use]`]: https://doc.rust-lang.org/reference/attributes/diagnostics.html#the-must_use-attribute
143 /// **Why is this bad?** Not bad at all, this lint just shows places where
144 /// you could add the attribute.
146 /// **Known problems:** The lint only checks the arguments for mutable
147 /// types without looking if they are actually changed. On the other hand,
148 /// it also ignores a broad range of potentially interesting side effects,
149 /// because we cannot decide whether the programmer intends the function to
150 /// be called for the side effect or the result. Expect many false
151 /// positives. At least we don't lint if the result type is unit or already
156 /// // this could be annotated with `#[must_use]`.
157 /// fn id<T>(t: T) -> T { t }
159 pub MUST_USE_CANDIDATE,
161 "function or method that could take a `#[must_use]` attribute"
164 #[derive(Copy, Clone)]
165 pub struct Functions {
171 pub fn new(threshold: u64, max_lines: u64) -> Self {
172 Self { threshold, max_lines }
176 impl_lint_pass!(Functions => [
179 NOT_UNSAFE_PTR_ARG_DEREF,
185 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Functions {
188 cx: &LateContext<'a, 'tcx>,
189 kind: intravisit::FnKind<'tcx>,
190 decl: &'tcx hir::FnDecl<'_>,
191 body: &'tcx hir::Body<'_>,
195 let is_impl = if let Some(hir::Node::Item(item)) = cx.tcx.hir().find(cx.tcx.hir().get_parent_node(hir_id)) {
196 matches!(item.kind, hir::ItemKind::Impl(_, _, _, _, Some(_), _, _))
201 let unsafety = match kind {
202 hir::intravisit::FnKind::ItemFn(_, _, hir::FnHeader { unsafety, .. }, _, _) => unsafety,
203 hir::intravisit::FnKind::Method(_, sig, _, _) => sig.header.unsafety,
204 hir::intravisit::FnKind::Closure(_) => return,
207 // don't warn for implementations, it's not their fault
209 // don't lint extern functions decls, it's not their fault either
211 hir::intravisit::FnKind::Method(
214 header: hir::FnHeader { abi: Abi::Rust, .. },
220 | hir::intravisit::FnKind::ItemFn(_, _, hir::FnHeader { abi: Abi::Rust, .. }, _, _) => {
221 self.check_arg_number(cx, decl, span.with_hi(decl.output.span().hi()))
227 Self::check_raw_ptr(cx, unsafety, decl, body, hir_id);
228 self.check_line_number(cx, span, body);
231 fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx hir::Item<'_>) {
232 let attr = must_use_attr(&item.attrs);
233 if let hir::ItemKind::Fn(ref sig, ref _generics, ref body_id) = item.kind {
234 if let Some(attr) = attr {
235 let fn_header_span = item.span.with_hi(sig.decl.output.span().hi());
236 check_needless_must_use(cx, &sig.decl, item.hir_id, item.span, fn_header_span, attr);
239 if cx.access_levels.is_exported(item.hir_id)
240 && !is_proc_macro(&item.attrs)
241 && attr_by_name(&item.attrs, "no_mangle").is_none()
243 check_must_use_candidate(
246 cx.tcx.hir().body(*body_id),
249 item.span.with_hi(sig.decl.output.span().hi()),
250 "this function could have a `#[must_use]` attribute",
256 fn check_impl_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx hir::ImplItem<'_>) {
257 if let hir::ImplItemKind::Method(ref sig, ref body_id) = item.kind {
258 let attr = must_use_attr(&item.attrs);
259 if let Some(attr) = attr {
260 let fn_header_span = item.span.with_hi(sig.decl.output.span().hi());
261 check_needless_must_use(cx, &sig.decl, item.hir_id, item.span, fn_header_span, attr);
262 } else if cx.access_levels.is_exported(item.hir_id)
263 && !is_proc_macro(&item.attrs)
264 && trait_ref_of_method(cx, item.hir_id).is_none()
266 check_must_use_candidate(
269 cx.tcx.hir().body(*body_id),
272 item.span.with_hi(sig.decl.output.span().hi()),
273 "this method could have a `#[must_use]` attribute",
279 fn check_trait_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx hir::TraitItem<'_>) {
280 if let hir::TraitItemKind::Method(ref sig, ref eid) = item.kind {
281 // don't lint extern functions decls, it's not their fault
282 if sig.header.abi == Abi::Rust {
283 self.check_arg_number(cx, &sig.decl, item.span.with_hi(sig.decl.output.span().hi()));
286 let attr = must_use_attr(&item.attrs);
287 if let Some(attr) = attr {
288 let fn_header_span = item.span.with_hi(sig.decl.output.span().hi());
289 check_needless_must_use(cx, &sig.decl, item.hir_id, item.span, fn_header_span, attr);
291 if let hir::TraitMethod::Provided(eid) = *eid {
292 let body = cx.tcx.hir().body(eid);
293 Self::check_raw_ptr(cx, sig.header.unsafety, &sig.decl, body, item.hir_id);
295 if attr.is_none() && cx.access_levels.is_exported(item.hir_id) && !is_proc_macro(&item.attrs) {
296 check_must_use_candidate(
302 item.span.with_hi(sig.decl.output.span().hi()),
303 "this method could have a `#[must_use]` attribute",
311 impl<'a, 'tcx> Functions {
312 fn check_arg_number(self, cx: &LateContext<'_, '_>, decl: &hir::FnDecl<'_>, fn_span: Span) {
313 let args = decl.inputs.len() as u64;
314 if args > self.threshold {
319 &format!("this function has too many arguments ({}/{})", args, self.threshold),
324 fn check_line_number(self, cx: &LateContext<'_, '_>, span: Span, body: &'tcx hir::Body<'_>) {
325 if in_external_macro(cx.sess(), span) {
329 let code_snippet = snippet(cx, body.value.span, "..");
330 let mut line_count: u64 = 0;
331 let mut in_comment = false;
332 let mut code_in_line;
334 // Skip the surrounding function decl.
335 let start_brace_idx = code_snippet.find('{').map_or(0, |i| i + 1);
336 let end_brace_idx = code_snippet.rfind('}').unwrap_or_else(|| code_snippet.len());
337 let function_lines = code_snippet[start_brace_idx..end_brace_idx].lines();
339 for mut line in function_lines {
340 code_in_line = false;
342 line = line.trim_start();
347 match line.find("*/") {
349 line = &line[i + 2..];
356 let multi_idx = line.find("/*").unwrap_or_else(|| line.len());
357 let single_idx = line.find("//").unwrap_or_else(|| line.len());
358 code_in_line |= multi_idx > 0 && single_idx > 0;
359 // Implies multi_idx is below line.len()
360 if multi_idx < single_idx {
361 line = &line[multi_idx + 2..];
373 if line_count > self.max_lines {
374 span_lint(cx, TOO_MANY_LINES, span, "This function has a large number of lines.")
379 cx: &LateContext<'a, 'tcx>,
380 unsafety: hir::Unsafety,
381 decl: &'tcx hir::FnDecl<'_>,
382 body: &'tcx hir::Body<'_>,
385 let expr = &body.value;
386 if unsafety == hir::Unsafety::Normal && cx.access_levels.is_exported(hir_id) {
387 let raw_ptrs = iter_input_pats(decl, body)
388 .zip(decl.inputs.iter())
389 .filter_map(|(arg, ty)| raw_ptr_arg(arg, ty))
390 .collect::<FxHashSet<_>>();
392 if !raw_ptrs.is_empty() {
393 let tables = cx.tcx.body_tables(body.id());
394 let mut v = DerefVisitor {
400 hir::intravisit::walk_expr(&mut v, expr);
406 fn check_needless_must_use(
407 cx: &LateContext<'_, '_>,
408 decl: &hir::FnDecl<'_>,
411 fn_header_span: Span,
414 if in_external_macro(cx.sess(), item_span) {
417 if returns_unit(decl) {
422 "this unit-returning function has a `#[must_use]` attribute",
426 "remove the attribute",
428 Applicability::MachineApplicable,
432 } else if !attr.is_value_str() && is_must_use_ty(cx, return_ty(cx, item_id)) {
437 "this function has an empty `#[must_use]` attribute, but returns a type already marked as `#[must_use]`",
438 "either add some descriptive text or remove the attribute",
443 fn check_must_use_candidate<'a, 'tcx>(
444 cx: &LateContext<'a, 'tcx>,
445 decl: &'tcx hir::FnDecl<'_>,
446 body: &'tcx hir::Body<'_>,
452 if has_mutable_arg(cx, body)
453 || mutates_static(cx, body)
454 || in_external_macro(cx.sess(), item_span)
455 || returns_unit(decl)
456 || !cx.access_levels.is_exported(item_id)
457 || is_must_use_ty(cx, return_ty(cx, item_id))
461 span_lint_and_then(cx, MUST_USE_CANDIDATE, fn_span, msg, |db| {
462 if let Some(snippet) = snippet_opt(cx, fn_span) {
466 format!("#[must_use] {}", snippet),
467 Applicability::MachineApplicable,
473 fn returns_unit(decl: &hir::FnDecl<'_>) -> bool {
475 hir::FunctionRetTy::DefaultReturn(_) => true,
476 hir::FunctionRetTy::Return(ref ty) => match ty.kind {
477 hir::TyKind::Tup(ref tys) => tys.is_empty(),
478 hir::TyKind::Never => true,
484 fn has_mutable_arg(cx: &LateContext<'_, '_>, body: &hir::Body<'_>) -> bool {
485 let mut tys = FxHashSet::default();
486 body.params.iter().any(|param| is_mutable_pat(cx, ¶m.pat, &mut tys))
489 fn is_mutable_pat(cx: &LateContext<'_, '_>, pat: &hir::Pat<'_>, tys: &mut FxHashSet<DefId>) -> bool {
490 if let hir::PatKind::Wild = pat.kind {
491 return false; // ignore `_` patterns
493 let def_id = pat.hir_id.owner_def_id();
494 if cx.tcx.has_typeck_tables(def_id) {
495 is_mutable_ty(cx, &cx.tcx.typeck_tables_of(def_id).pat_ty(pat), pat.span, tys)
501 static KNOWN_WRAPPER_TYS: &[&[&str]] = &[&["alloc", "rc", "Rc"], &["std", "sync", "Arc"]];
503 fn is_mutable_ty<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, ty: Ty<'tcx>, span: Span, tys: &mut FxHashSet<DefId>) -> bool {
506 // primitive types are never mutable
507 Bool | Char | Int(_) | Uint(_) | Float(_) | Str => false,
508 Adt(ref adt, ref substs) => {
509 tys.insert(adt.did) && !ty.is_freeze(cx.tcx, cx.param_env, span)
510 || KNOWN_WRAPPER_TYS.iter().any(|path| match_def_path(cx, adt.did, path))
511 && substs.types().any(|ty| is_mutable_ty(cx, ty, span, tys))
513 Tuple(ref substs) => substs.types().any(|ty| is_mutable_ty(cx, ty, span, tys)),
514 Array(ty, _) | Slice(ty) => is_mutable_ty(cx, ty, span, tys),
515 RawPtr(ty::TypeAndMut { ty, mutbl }) | Ref(_, ty, mutbl) => {
516 mutbl == hir::Mutability::Mut || is_mutable_ty(cx, ty, span, tys)
518 // calling something constitutes a side effect, so return true on all callables
519 // also never calls need not be used, so return true for them, too
524 fn raw_ptr_arg(arg: &hir::Param<'_>, ty: &hir::Ty<'_>) -> Option<hir::HirId> {
525 if let (&hir::PatKind::Binding(_, id, _, _), &hir::TyKind::Ptr(_)) = (&arg.pat.kind, &ty.kind) {
532 struct DerefVisitor<'a, 'tcx> {
533 cx: &'a LateContext<'a, 'tcx>,
534 ptrs: FxHashSet<hir::HirId>,
535 tables: &'a ty::TypeckTables<'tcx>,
538 impl<'a, 'tcx> intravisit::Visitor<'tcx> for DerefVisitor<'a, 'tcx> {
539 fn visit_expr(&mut self, expr: &'tcx hir::Expr<'_>) {
541 hir::ExprKind::Call(ref f, args) => {
542 let ty = self.tables.expr_ty(f);
544 if type_is_unsafe_function(self.cx, ty) {
550 hir::ExprKind::MethodCall(_, _, args) => {
551 let def_id = self.tables.type_dependent_def_id(expr.hir_id).unwrap();
552 let base_type = self.cx.tcx.type_of(def_id);
554 if type_is_unsafe_function(self.cx, base_type) {
560 hir::ExprKind::Unary(hir::UnDeref, ref ptr) => self.check_arg(ptr),
564 intravisit::walk_expr(self, expr);
567 fn nested_visit_map<'this>(&'this mut self) -> intravisit::NestedVisitorMap<'this, 'tcx> {
568 intravisit::NestedVisitorMap::None
572 impl<'a, 'tcx> DerefVisitor<'a, 'tcx> {
573 fn check_arg(&self, ptr: &hir::Expr<'_>) {
574 if let hir::ExprKind::Path(ref qpath) = ptr.kind {
575 if let Res::Local(id) = qpath_res(self.cx, qpath, ptr.hir_id) {
576 if self.ptrs.contains(&id) {
579 NOT_UNSAFE_PTR_ARG_DEREF,
581 "this public function dereferences a raw pointer but is not marked `unsafe`",
589 struct StaticMutVisitor<'a, 'tcx> {
590 cx: &'a LateContext<'a, 'tcx>,
591 mutates_static: bool,
594 impl<'a, 'tcx> intravisit::Visitor<'tcx> for StaticMutVisitor<'a, 'tcx> {
595 fn visit_expr(&mut self, expr: &'tcx hir::Expr<'_>) {
596 use hir::ExprKind::*;
598 if self.mutates_static {
602 Call(_, args) | MethodCall(_, _, args) => {
603 let mut tys = FxHashSet::default();
605 let def_id = arg.hir_id.owner_def_id();
606 if self.cx.tcx.has_typeck_tables(def_id)
609 self.cx.tcx.typeck_tables_of(def_id).expr_ty(arg),
613 && is_mutated_static(self.cx, arg)
615 self.mutates_static = true;
621 Assign(ref target, ..) | AssignOp(_, ref target, _) | AddrOf(_, hir::Mutability::Mut, ref target) => {
622 self.mutates_static |= is_mutated_static(self.cx, target)
628 fn nested_visit_map<'this>(&'this mut self) -> intravisit::NestedVisitorMap<'this, 'tcx> {
629 intravisit::NestedVisitorMap::None
633 fn is_mutated_static(cx: &LateContext<'_, '_>, e: &hir::Expr<'_>) -> bool {
634 use hir::ExprKind::*;
638 if let Res::Local(_) = qpath_res(cx, qpath, e.hir_id) {
644 Field(ref inner, _) | Index(ref inner, _) => is_mutated_static(cx, inner),
649 fn mutates_static<'a, 'tcx>(cx: &'a LateContext<'a, 'tcx>, body: &'tcx hir::Body<'_>) -> bool {
650 let mut v = StaticMutVisitor {
652 mutates_static: false,
654 intravisit::walk_expr(&mut v, &body.value);