|
| 1 | +use clippy_utils::diagnostics::span_lint_and_then; |
| 2 | +use rustc_hir::def_id::LocalDefId; |
| 3 | +use rustc_hir::intravisit::FnKind; |
| 4 | +use rustc_hir::{Body, ExprKind, FnDecl, FnRetTy, TyKind}; |
| 5 | +use rustc_lint::{LateContext, LateLintPass}; |
| 6 | +use rustc_session::declare_lint_pass; |
| 7 | +use rustc_span::{BytePos, Span}; |
| 8 | + |
| 9 | +declare_clippy_lint! { |
| 10 | + /// ### What it does |
| 11 | + /// Checks for function whose return type is an `impl` trait |
| 12 | + /// implemented by `()`, and whose `()` return value is implicit. |
| 13 | + /// |
| 14 | + /// ### Why is this bad? |
| 15 | + /// Since the required trait is implemented by `()`, adding an extra `;` |
| 16 | + /// at the end of the function last expression will compile with no |
| 17 | + /// indication that the expected value may have been silently swallowed. |
| 18 | + /// |
| 19 | + /// ### Example |
| 20 | + /// ```no_run |
| 21 | + /// fn key() -> impl Eq { |
| 22 | + /// [1, 10, 2, 0].sort_unstable() |
| 23 | + /// } |
| 24 | + /// ``` |
| 25 | + /// Use instead: |
| 26 | + /// ```no_run |
| 27 | + /// fn key() -> impl Eq { |
| 28 | + /// let mut arr = [1, 10, 2, 0]; |
| 29 | + /// arr.sort_unstable(); |
| 30 | + /// arr |
| 31 | + /// } |
| 32 | + /// ``` |
| 33 | + /// or |
| 34 | + /// ```no_run |
| 35 | + /// fn key() -> impl Eq { |
| 36 | + /// [1, 10, 2, 0].sort_unstable(); |
| 37 | + /// () |
| 38 | + /// } |
| 39 | + /// ``` |
| 40 | + /// if returning `()` is intentional. |
| 41 | + #[clippy::version = "1.86.0"] |
| 42 | + pub UNIT_AS_IMPL_TRAIT, |
| 43 | + suspicious, |
| 44 | + "`()` which implements the required trait might be returned unexpectedly" |
| 45 | +} |
| 46 | + |
| 47 | +declare_lint_pass!(UnitAsImplTrait => [UNIT_AS_IMPL_TRAIT]); |
| 48 | + |
| 49 | +impl<'tcx> LateLintPass<'tcx> for UnitAsImplTrait { |
| 50 | + fn check_fn( |
| 51 | + &mut self, |
| 52 | + cx: &LateContext<'tcx>, |
| 53 | + _: FnKind<'tcx>, |
| 54 | + decl: &'tcx FnDecl<'tcx>, |
| 55 | + body: &'tcx Body<'tcx>, |
| 56 | + span: Span, |
| 57 | + _: LocalDefId, |
| 58 | + ) { |
| 59 | + if !span.from_expansion() |
| 60 | + && let FnRetTy::Return(ty) = decl.output |
| 61 | + && let TyKind::OpaqueDef(_) = ty.kind |
| 62 | + && cx.typeck_results().expr_ty(body.value).is_unit() |
| 63 | + && let ExprKind::Block(block, None) = body.value.kind |
| 64 | + && block.expr.is_none_or(|e| !matches!(e.kind, ExprKind::Tup([]))) |
| 65 | + { |
| 66 | + let block_end_span = block.span.with_hi(block.span.hi() - BytePos(1)).shrink_to_hi(); |
| 67 | + |
| 68 | + span_lint_and_then( |
| 69 | + cx, |
| 70 | + UNIT_AS_IMPL_TRAIT, |
| 71 | + ty.span, |
| 72 | + "this function returns `()` which implements the required trait", |
| 73 | + |diag| { |
| 74 | + if let Some(expr) = block.expr { |
| 75 | + diag.span_note(expr.span, "this expression evaluates to `()`") |
| 76 | + .span_help( |
| 77 | + expr.span.shrink_to_hi(), |
| 78 | + "consider being explicit, and terminate the body with `()`", |
| 79 | + ); |
| 80 | + } else if let Some(last) = block.stmts.last() { |
| 81 | + diag.span_note(last.span, "this statement evaluates to `()` because it ends with `;`") |
| 82 | + .span_note(block.span, "therefore the function body evaluates to `()`") |
| 83 | + .span_help( |
| 84 | + block_end_span, |
| 85 | + "if this is intentional, consider being explicit, and terminate the body with `()`", |
| 86 | + ); |
| 87 | + } else { |
| 88 | + diag.span_note(block.span, "the empty body evaluates to `()`") |
| 89 | + .span_help(block_end_span, "consider being explicit and use `()` in the body"); |
| 90 | + } |
| 91 | + }, |
| 92 | + ); |
| 93 | + } |
| 94 | + } |
| 95 | +} |
0 commit comments