2 use crate::{EarlyContext, EarlyLintPass, LateContext, LateLintPass, LintContext};
4 use rustc_ast::util::{classify, parser};
5 use rustc_ast::{ExprKind, StmtKind};
6 use rustc_errors::{fluent, pluralize, Applicability, MultiSpan};
8 use rustc_hir::def::{DefKind, Res};
9 use rustc_hir::def_id::DefId;
10 use rustc_infer::traits::util::elaborate_predicates_with_span;
11 use rustc_middle::ty::adjustment;
12 use rustc_middle::ty::{self, DefIdTree, Ty};
13 use rustc_span::symbol::Symbol;
14 use rustc_span::symbol::{kw, sym};
15 use rustc_span::{BytePos, Span};
19 /// The `unused_must_use` lint detects unused result of a type flagged as
25 /// fn returns_result() -> Result<(), ()> {
38 /// The `#[must_use]` attribute is an indicator that it is a mistake to
39 /// ignore the value. See [the reference] for more details.
41 /// [the reference]: https://doc.rust-lang.org/reference/attributes/diagnostics.html#the-must_use-attribute
44 "unused result of a type flagged as `#[must_use]`",
45 report_in_external_macro
49 /// The `unused_results` lint checks for the unused result of an
50 /// expression in a statement.
54 /// ```rust,compile_fail
55 /// #![deny(unused_results)]
56 /// fn foo<T>() -> T { panic!() }
67 /// Ignoring the return value of a function may indicate a mistake. In
68 /// cases were it is almost certain that the result should be used, it is
69 /// recommended to annotate the function with the [`must_use` attribute].
70 /// Failure to use such a return value will trigger the [`unused_must_use`
71 /// lint] which is warn-by-default. The `unused_results` lint is
72 /// essentially the same, but triggers for *all* return values.
74 /// This lint is "allow" by default because it can be noisy, and may not be
75 /// an actual problem. For example, calling the `remove` method of a `Vec`
76 /// or `HashMap` returns the previous value, which you may not care about.
77 /// Using this lint would require explicitly ignoring or discarding such
80 /// [`must_use` attribute]: https://doc.rust-lang.org/reference/attributes/diagnostics.html#the-must_use-attribute
81 /// [`unused_must_use` lint]: warn-by-default.html#unused-must-use
84 "unused result of an expression in a statement"
87 declare_lint_pass!(UnusedResults => [UNUSED_MUST_USE, UNUSED_RESULTS]);
89 impl<'tcx> LateLintPass<'tcx> for UnusedResults {
90 fn check_stmt(&mut self, cx: &LateContext<'_>, s: &hir::Stmt<'_>) {
91 let hir::StmtKind::Semi(expr) = s.kind else { return; };
93 if let hir::ExprKind::Ret(..) = expr.kind {
97 if let hir::ExprKind::Match(await_expr, _arms, hir::MatchSource::AwaitDesugar) = expr.kind
98 && let ty = cx.typeck_results().expr_ty(&await_expr)
99 && let ty::Opaque(future_def_id, _) = ty.kind()
100 && cx.tcx.ty_is_opaque_future(ty)
101 // FIXME: This also includes non-async fns that return `impl Future`.
102 && let async_fn_def_id = cx.tcx.parent(*future_def_id)
103 && check_must_use_def(
107 "output of future returned by ",
111 // We have a bare `foo().await;` on an opaque type from an async function that was
112 // annotated with `#[must_use]`.
116 let ty = cx.typeck_results().expr_ty(&expr);
118 let must_use_result = is_ty_must_use(cx, ty, &expr, expr.span);
119 let type_lint_emitted_or_suppressed = match must_use_result {
121 emit_must_use_untranslated(cx, &path, "", "", 1);
127 let fn_warned = check_fn_must_use(cx, expr);
129 if !fn_warned && type_lint_emitted_or_suppressed {
130 // We don't warn about unused unit or uninhabited types.
131 // (See https://github.com/rust-lang/rust/issues/43806 for details.)
135 let must_use_op = match expr.kind {
136 // Hardcoding operators here seemed more expedient than the
137 // refactoring that would be needed to look up the `#[must_use]`
138 // attribute which does exist on the comparison trait methods
139 hir::ExprKind::Binary(bin_op, ..) => match bin_op.node {
145 | hir::BinOpKind::Gt => Some("comparison"),
147 | hir::BinOpKind::Sub
148 | hir::BinOpKind::Div
149 | hir::BinOpKind::Mul
150 | hir::BinOpKind::Rem => Some("arithmetic operation"),
151 hir::BinOpKind::And | hir::BinOpKind::Or => Some("logical operation"),
152 hir::BinOpKind::BitXor
153 | hir::BinOpKind::BitAnd
154 | hir::BinOpKind::BitOr
155 | hir::BinOpKind::Shl
156 | hir::BinOpKind::Shr => Some("bitwise operation"),
158 hir::ExprKind::AddrOf(..) => Some("borrow"),
159 hir::ExprKind::Unary(..) => Some("unary operation"),
163 let mut op_warned = false;
165 if let Some(must_use_op) = must_use_op {
166 cx.struct_span_lint(UNUSED_MUST_USE, expr.span, fluent::lint_unused_op, |lint| {
167 lint.set_arg("op", must_use_op)
168 .span_label(expr.span, fluent::label)
169 .span_suggestion_verbose(
170 expr.span.shrink_to_lo(),
173 Applicability::MachineApplicable,
179 if !(type_lint_emitted_or_suppressed || fn_warned || op_warned) {
180 cx.struct_span_lint(UNUSED_RESULTS, s.span, fluent::lint_unused_result, |lint| {
181 lint.set_arg("ty", ty)
185 fn check_fn_must_use(cx: &LateContext<'_>, expr: &hir::Expr<'_>) -> bool {
186 let maybe_def_id = match expr.kind {
187 hir::ExprKind::Call(ref callee, _) => {
189 hir::ExprKind::Path(ref qpath) => {
190 match cx.qpath_res(qpath, callee.hir_id) {
191 Res::Def(DefKind::Fn | DefKind::AssocFn, def_id) => Some(def_id),
192 // `Res::Local` if it was a closure, for which we
193 // do not currently support must-use linting
200 hir::ExprKind::MethodCall(..) => {
201 cx.typeck_results().type_dependent_def_id(expr.hir_id)
205 if let Some(def_id) = maybe_def_id {
206 check_must_use_def(cx, def_id, expr.span, "return value of ", "")
212 /// A path through a type to a must_use source. Contains useful info for the lint.
215 /// Suppress must_use checking.
217 /// The root of the normal must_use lint with an optional message.
218 Def(Span, DefId, Option<Symbol>),
221 TraitObject(Box<Self>),
222 TupleElement(Vec<(usize, Self)>),
223 Array(Box<Self>, u64),
224 /// The root of the unused_closures lint.
226 /// The root of the unused_generators lint.
230 #[instrument(skip(cx, expr), level = "debug", ret)]
231 fn is_ty_must_use<'tcx>(
232 cx: &LateContext<'tcx>,
234 expr: &hir::Expr<'_>,
236 ) -> Option<MustUsePath> {
238 || !ty.is_inhabited_from(
240 cx.tcx.parent_module(expr.hir_id).to_def_id(),
244 return Some(MustUsePath::Suppressed);
248 ty::Adt(..) if ty.is_box() => {
249 let boxed_ty = ty.boxed_ty();
250 is_ty_must_use(cx, boxed_ty, expr, span)
251 .map(|inner| MustUsePath::Boxed(Box::new(inner)))
253 ty::Adt(def, _) => is_def_must_use(cx, def.did(), span),
254 ty::Opaque(def, _) => {
255 elaborate_predicates_with_span(
257 cx.tcx.explicit_item_bounds(def).iter().cloned(),
259 .filter_map(|obligation| {
260 // We only look at the `DefId`, so it is safe to skip the binder here.
261 if let ty::PredicateKind::Clause(ty::Clause::Trait(
262 ref poly_trait_predicate,
263 )) = obligation.predicate.kind().skip_binder()
265 let def_id = poly_trait_predicate.trait_ref.def_id;
267 is_def_must_use(cx, def_id, span)
272 .map(|inner| MustUsePath::Opaque(Box::new(inner)))
275 ty::Dynamic(binders, _, _) => binders
277 .filter_map(|predicate| {
278 if let ty::ExistentialPredicate::Trait(ref trait_ref) =
279 predicate.skip_binder()
281 let def_id = trait_ref.def_id;
282 is_def_must_use(cx, def_id, span)
286 .map(|inner| MustUsePath::TraitObject(Box::new(inner)))
290 let elem_exprs = if let hir::ExprKind::Tup(elem_exprs) = expr.kind {
291 debug_assert_eq!(elem_exprs.len(), tys.len());
297 // Default to `expr`.
298 let elem_exprs = elem_exprs.iter().chain(iter::repeat(expr));
300 let nested_must_use = tys
304 .filter_map(|(i, (ty, expr))| {
305 is_ty_must_use(cx, ty, expr, expr.span).map(|path| (i, path))
307 .collect::<Vec<_>>();
309 if !nested_must_use.is_empty() {
310 Some(MustUsePath::TupleElement(nested_must_use))
315 ty::Array(ty, len) => match len.try_eval_usize(cx.tcx, cx.param_env) {
316 // If the array is empty we don't lint, to avoid false positives
317 Some(0) | None => None,
318 // If the array is definitely non-empty, we can do `#[must_use]` checking.
319 Some(len) => is_ty_must_use(cx, ty, expr, span)
320 .map(|inner| MustUsePath::Array(Box::new(inner), len)),
322 ty::Closure(..) => Some(MustUsePath::Closure(span)),
323 ty::Generator(def_id, ..) => {
324 // async fn should be treated as "implementor of `Future`"
325 let must_use = if cx.tcx.generator_is_async(def_id) {
326 let def_id = cx.tcx.lang_items().future_trait().unwrap();
327 is_def_must_use(cx, def_id, span)
328 .map(|inner| MustUsePath::Opaque(Box::new(inner)))
332 must_use.or(Some(MustUsePath::Generator(span)))
338 fn is_def_must_use(cx: &LateContext<'_>, def_id: DefId, span: Span) -> Option<MustUsePath> {
339 if let Some(attr) = cx.tcx.get_attr(def_id, sym::must_use) {
340 // check for #[must_use = "..."]
341 let reason = attr.value_str();
342 Some(MustUsePath::Def(span, def_id, reason))
348 // Returns whether further errors should be suppressed because either a lint has been emitted or the type should be ignored.
349 fn check_must_use_def(
350 cx: &LateContext<'_>,
353 descr_pre_path: &str,
354 descr_post_path: &str,
356 is_def_must_use(cx, def_id, span)
357 .map(|must_use_path| {
358 emit_must_use_untranslated(
369 #[instrument(skip(cx), level = "debug")]
370 fn emit_must_use_untranslated(
371 cx: &LateContext<'_>,
377 let plural_suffix = pluralize!(plural_len);
380 MustUsePath::Suppressed => {}
381 MustUsePath::Boxed(path) => {
382 let descr_pre = &format!("{}boxed ", descr_pre);
383 emit_must_use_untranslated(cx, path, descr_pre, descr_post, plural_len);
385 MustUsePath::Opaque(path) => {
386 let descr_pre = &format!("{}implementer{} of ", descr_pre, plural_suffix);
387 emit_must_use_untranslated(cx, path, descr_pre, descr_post, plural_len);
389 MustUsePath::TraitObject(path) => {
390 let descr_post = &format!(" trait object{}{}", plural_suffix, descr_post);
391 emit_must_use_untranslated(cx, path, descr_pre, descr_post, plural_len);
393 MustUsePath::TupleElement(elems) => {
394 for (index, path) in elems {
395 let descr_post = &format!(" in tuple element {}", index);
396 emit_must_use_untranslated(cx, path, descr_pre, descr_post, plural_len);
399 MustUsePath::Array(path, len) => {
400 let descr_pre = &format!("{}array{} of ", descr_pre, plural_suffix);
401 emit_must_use_untranslated(
406 plural_len.saturating_add(usize::try_from(*len).unwrap_or(usize::MAX)),
409 MustUsePath::Closure(span) => {
413 fluent::lint_unused_closure,
415 // FIXME(davidtwco): this isn't properly translatable because of the
417 lint.set_arg("count", plural_len)
418 .set_arg("pre", descr_pre)
419 .set_arg("post", descr_post)
424 MustUsePath::Generator(span) => {
428 fluent::lint_unused_generator,
430 // FIXME(davidtwco): this isn't properly translatable because of the
432 lint.set_arg("count", plural_len)
433 .set_arg("pre", descr_pre)
434 .set_arg("post", descr_post)
439 MustUsePath::Def(span, def_id, reason) => {
440 cx.struct_span_lint(UNUSED_MUST_USE, *span, fluent::lint_unused_def, |lint| {
441 // FIXME(davidtwco): this isn't properly translatable because of the pre/post
443 lint.set_arg("pre", descr_pre);
444 lint.set_arg("post", descr_post);
445 lint.set_arg("def", cx.tcx.def_path_str(*def_id));
446 if let Some(note) = reason {
447 lint.note(note.as_str());
458 /// The `path_statements` lint detects path statements with no effect.
472 /// It is usually a mistake to have a statement that has no effect.
475 "path statements with no effect"
478 declare_lint_pass!(PathStatements => [PATH_STATEMENTS]);
480 impl<'tcx> LateLintPass<'tcx> for PathStatements {
481 fn check_stmt(&mut self, cx: &LateContext<'_>, s: &hir::Stmt<'_>) {
482 if let hir::StmtKind::Semi(expr) = s.kind {
483 if let hir::ExprKind::Path(_) = expr.kind {
484 let ty = cx.typeck_results().expr_ty(expr);
485 if ty.needs_drop(cx.tcx, cx.param_env) {
489 fluent::lint_path_statement_drop,
491 if let Ok(snippet) = cx.sess().source_map().span_to_snippet(expr.span) {
492 lint.span_suggestion(
495 format!("drop({});", snippet),
496 Applicability::MachineApplicable,
499 lint.span_help(s.span, fluent::suggestion);
508 fluent::lint_path_statement_no_effect,
517 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
518 enum UnusedDelimsCtx {
522 AssignedValueLetElse,
535 impl From<UnusedDelimsCtx> for &'static str {
536 fn from(ctx: UnusedDelimsCtx) -> &'static str {
538 UnusedDelimsCtx::FunctionArg => "function argument",
539 UnusedDelimsCtx::MethodArg => "method argument",
540 UnusedDelimsCtx::AssignedValue | UnusedDelimsCtx::AssignedValueLetElse => {
543 UnusedDelimsCtx::IfCond => "`if` condition",
544 UnusedDelimsCtx::WhileCond => "`while` condition",
545 UnusedDelimsCtx::ForIterExpr => "`for` iterator expression",
546 UnusedDelimsCtx::MatchScrutineeExpr => "`match` scrutinee expression",
547 UnusedDelimsCtx::ReturnValue => "`return` value",
548 UnusedDelimsCtx::BlockRetValue => "block return value",
549 UnusedDelimsCtx::LetScrutineeExpr => "`let` scrutinee expression",
550 UnusedDelimsCtx::ArrayLenExpr | UnusedDelimsCtx::AnonConst => "const expression",
551 UnusedDelimsCtx::MatchArmExpr => "match arm expression",
556 /// Used by both `UnusedParens` and `UnusedBraces` to prevent code duplication.
557 trait UnusedDelimLint {
558 const DELIM_STR: &'static str;
560 /// Due to `ref` pattern, there can be a difference between using
561 /// `{ expr }` and `expr` in pattern-matching contexts. This means
562 /// that we should only lint `unused_parens` and not `unused_braces`
567 /// let ref b = { a }; // We actually borrow a copy of `a` here.
568 /// a += 1; // By mutating `a` we invalidate any borrows of `a`.
569 /// assert_eq!(b + 1, a); // `b` does not borrow `a`, so we can still use it here.
571 const LINT_EXPR_IN_PATTERN_MATCHING_CTX: bool;
573 // this cannot be a constant is it refers to a static.
574 fn lint(&self) -> &'static Lint;
576 fn check_unused_delims_expr(
578 cx: &EarlyContext<'_>,
580 ctx: UnusedDelimsCtx,
581 followed_by_block: bool,
582 left_pos: Option<BytePos>,
583 right_pos: Option<BytePos>,
586 fn is_expr_delims_necessary(
588 followed_by_block: bool,
589 followed_by_else: bool,
591 if followed_by_else {
593 ast::ExprKind::Binary(op, ..) if op.node.lazy() => return true,
594 _ if classify::expr_trailing_brace(inner).is_some() => return true,
599 // Prevent false-positives in cases like `fn x() -> u8 { ({ 0 } + 1) }`
600 let lhs_needs_parens = {
601 let mut innermost = inner;
603 innermost = match &innermost.kind {
604 ExprKind::Binary(_, lhs, _rhs) => lhs,
605 ExprKind::Call(fn_, _params) => fn_,
606 ExprKind::Cast(expr, _ty) => expr,
607 ExprKind::Type(expr, _ty) => expr,
608 ExprKind::Index(base, _subscript) => base,
611 if !classify::expr_requires_semi_to_be_stmt(innermost) {
618 || (followed_by_block
619 && match &inner.kind {
621 | ExprKind::Break(..)
622 | ExprKind::Yield(..)
623 | ExprKind::Yeet(..) => true,
624 ExprKind::Range(_lhs, Some(rhs), _limits) => {
625 matches!(rhs.kind, ExprKind::Block(..))
627 _ => parser::contains_exterior_struct_lit(&inner),
631 fn emit_unused_delims_expr(
633 cx: &EarlyContext<'_>,
635 ctx: UnusedDelimsCtx,
636 left_pos: Option<BytePos>,
637 right_pos: Option<BytePos>,
639 let spans = match value.kind {
640 ast::ExprKind::Block(ref block, None) if block.stmts.len() == 1 => {
641 if let StmtKind::Expr(expr) = &block.stmts[0].kind
642 && let ExprKind::Err = expr.kind
646 if let Some(span) = block.stmts[0].span.find_ancestor_inside(value.span) {
647 Some((value.span.with_hi(span.lo()), value.span.with_lo(span.hi())))
652 ast::ExprKind::Paren(ref expr) => {
653 let expr_span = expr.span.find_ancestor_inside(value.span);
654 if let Some(expr_span) = expr_span {
655 Some((value.span.with_hi(expr_span.lo()), value.span.with_lo(expr_span.hi())))
663 left_pos.map_or(false, |s| s >= value.span.lo()),
664 right_pos.map_or(false, |s| s <= value.span.hi()),
666 self.emit_unused_delims(cx, value.span, spans, ctx.into(), keep_space);
669 fn emit_unused_delims(
671 cx: &EarlyContext<'_>,
673 spans: Option<(Span, Span)>,
675 keep_space: (bool, bool),
677 let primary_span = if let Some((lo, hi)) = spans {
678 MultiSpan::from(vec![lo, hi])
680 MultiSpan::from(value_span)
682 cx.struct_span_lint(self.lint(), primary_span, fluent::lint_unused_delim, |lint| {
683 lint.set_arg("delim", Self::DELIM_STR);
684 lint.set_arg("item", msg);
685 if let Some((lo, hi)) = spans {
686 let sm = cx.sess().source_map();
689 let Ok(snip) = sm.span_to_prev_source(lo) && !snip.ends_with(' ') {
697 let Ok(snip) = sm.span_to_next_source(hi) && !snip.starts_with(' ') {
703 let replacement = vec![(lo, lo_replace), (hi, hi_replace)];
704 lint.multipart_suggestion(
707 Applicability::MachineApplicable,
714 fn check_expr(&mut self, cx: &EarlyContext<'_>, e: &ast::Expr) {
715 use rustc_ast::ExprKind::*;
716 let (value, ctx, followed_by_block, left_pos, right_pos) = match e.kind {
717 // Do not lint `unused_braces` in `if let` expressions.
718 If(ref cond, ref block, _)
719 if !matches!(cond.kind, Let(_, _, _))
720 || Self::LINT_EXPR_IN_PATTERN_MATCHING_CTX =>
722 let left = e.span.lo() + rustc_span::BytePos(2);
723 let right = block.span.lo();
724 (cond, UnusedDelimsCtx::IfCond, true, Some(left), Some(right))
727 // Do not lint `unused_braces` in `while let` expressions.
728 While(ref cond, ref block, ..)
729 if !matches!(cond.kind, Let(_, _, _))
730 || Self::LINT_EXPR_IN_PATTERN_MATCHING_CTX =>
732 let left = e.span.lo() + rustc_span::BytePos(5);
733 let right = block.span.lo();
734 (cond, UnusedDelimsCtx::WhileCond, true, Some(left), Some(right))
737 ForLoop(_, ref cond, ref block, ..) => {
738 (cond, UnusedDelimsCtx::ForIterExpr, true, None, Some(block.span.lo()))
741 Match(ref head, _) if Self::LINT_EXPR_IN_PATTERN_MATCHING_CTX => {
742 let left = e.span.lo() + rustc_span::BytePos(5);
743 (head, UnusedDelimsCtx::MatchScrutineeExpr, true, Some(left), None)
746 Ret(Some(ref value)) => {
747 let left = e.span.lo() + rustc_span::BytePos(3);
748 (value, UnusedDelimsCtx::ReturnValue, false, Some(left), None)
751 Assign(_, ref value, _) | AssignOp(.., ref value) => {
752 (value, UnusedDelimsCtx::AssignedValue, false, None, None)
754 // either function/method call, or something this lint doesn't care about
755 ref call_or_other => {
756 let (args_to_check, ctx) = match *call_or_other {
757 Call(_, ref args) => (&args[..], UnusedDelimsCtx::FunctionArg),
758 MethodCall(ref call) => (&call.args[..], UnusedDelimsCtx::MethodArg),
759 // actual catch-all arm
764 // Don't lint if this is a nested macro expansion: otherwise, the lint could
765 // trigger in situations that macro authors shouldn't have to care about, e.g.,
766 // when a parenthesized token tree matched in one macro expansion is matched as
767 // an expression in another and used as a fn/method argument (Issue #47775)
768 if e.span.ctxt().outer_expn_data().call_site.from_expansion() {
771 for arg in args_to_check {
772 self.check_unused_delims_expr(cx, arg, ctx, false, None, None);
777 self.check_unused_delims_expr(cx, &value, ctx, followed_by_block, left_pos, right_pos);
780 fn check_stmt(&mut self, cx: &EarlyContext<'_>, s: &ast::Stmt) {
782 StmtKind::Local(ref local) if Self::LINT_EXPR_IN_PATTERN_MATCHING_CTX => {
783 if let Some((init, els)) = local.kind.init_else_opt() {
784 let ctx = match els {
785 None => UnusedDelimsCtx::AssignedValue,
786 Some(_) => UnusedDelimsCtx::AssignedValueLetElse,
788 self.check_unused_delims_expr(cx, init, ctx, false, None, None);
791 StmtKind::Expr(ref expr) => {
792 self.check_unused_delims_expr(
795 UnusedDelimsCtx::BlockRetValue,
805 fn check_item(&mut self, cx: &EarlyContext<'_>, item: &ast::Item) {
806 use ast::ItemKind::*;
808 if let Const(.., Some(expr)) | Static(.., Some(expr)) = &item.kind {
809 self.check_unused_delims_expr(
812 UnusedDelimsCtx::AssignedValue,
822 /// The `unused_parens` lint detects `if`, `match`, `while` and `return`
823 /// with parentheses; they do not need them.
835 /// The parentheses are not needed, and should be removed. This is the
836 /// preferred style for writing these expressions.
837 pub(super) UNUSED_PARENS,
839 "`if`, `match`, `while` and `return` do not need parentheses"
842 declare_lint_pass!(UnusedParens => [UNUSED_PARENS]);
844 impl UnusedDelimLint for UnusedParens {
845 const DELIM_STR: &'static str = "parentheses";
847 const LINT_EXPR_IN_PATTERN_MATCHING_CTX: bool = true;
849 fn lint(&self) -> &'static Lint {
853 fn check_unused_delims_expr(
855 cx: &EarlyContext<'_>,
857 ctx: UnusedDelimsCtx,
858 followed_by_block: bool,
859 left_pos: Option<BytePos>,
860 right_pos: Option<BytePos>,
863 ast::ExprKind::Paren(ref inner) => {
864 let followed_by_else = ctx == UnusedDelimsCtx::AssignedValueLetElse;
865 if !Self::is_expr_delims_necessary(inner, followed_by_block, followed_by_else)
866 && value.attrs.is_empty()
867 && !value.span.from_expansion()
868 && (ctx != UnusedDelimsCtx::LetScrutineeExpr
869 || !matches!(inner.kind, ast::ExprKind::Binary(
870 rustc_span::source_map::Spanned { node, .. },
875 self.emit_unused_delims_expr(cx, value, ctx, left_pos, right_pos)
878 ast::ExprKind::Let(_, ref expr, _) => {
879 self.check_unused_delims_expr(
882 UnusedDelimsCtx::LetScrutineeExpr,
894 fn check_unused_parens_pat(
896 cx: &EarlyContext<'_>,
900 keep_space: (bool, bool),
902 use ast::{BindingAnnotation, PatKind};
904 if let PatKind::Paren(inner) = &value.kind {
906 // The lint visitor will visit each subpattern of `p`. We do not want to lint
907 // any range pattern no matter where it occurs in the pattern. For something like
908 // `&(a..=b)`, there is a recursive `check_pat` on `a` and `b`, but we will assume
909 // that if there are unnecessary parens they serve a purpose of readability.
910 PatKind::Range(..) => return,
911 // Avoid `p0 | .. | pn` if we should.
912 PatKind::Or(..) if avoid_or => return,
913 // Avoid `mut x` and `mut x @ p` if we should:
914 PatKind::Ident(BindingAnnotation::MUT, ..) if avoid_mut => {
917 // Otherwise proceed with linting.
920 let spans = if let Some(inner) = inner.span.find_ancestor_inside(value.span) {
921 Some((value.span.with_hi(inner.lo()), value.span.with_lo(inner.hi())))
925 self.emit_unused_delims(cx, value.span, spans, "pattern", keep_space);
930 impl EarlyLintPass for UnusedParens {
931 fn check_expr(&mut self, cx: &EarlyContext<'_>, e: &ast::Expr) {
933 ExprKind::Let(ref pat, _, _) | ExprKind::ForLoop(ref pat, ..) => {
934 self.check_unused_parens_pat(cx, pat, false, false, (true, true));
936 // We ignore parens in cases like `if (((let Some(0) = Some(1))))` because we already
937 // handle a hard error for them during AST lowering in `lower_expr_mut`, but we still
938 // want to complain about things like `if let 42 = (42)`.
939 ExprKind::If(ref cond, ref block, ref else_)
940 if matches!(cond.peel_parens().kind, ExprKind::Let(..)) =>
942 self.check_unused_delims_expr(
945 UnusedDelimsCtx::LetScrutineeExpr,
950 for stmt in &block.stmts {
951 <Self as UnusedDelimLint>::check_stmt(self, cx, stmt);
953 if let Some(e) = else_ {
954 <Self as UnusedDelimLint>::check_expr(self, cx, e);
958 ExprKind::Match(ref _expr, ref arm) => {
960 self.check_unused_delims_expr(
963 UnusedDelimsCtx::MatchArmExpr,
973 <Self as UnusedDelimLint>::check_expr(self, cx, e)
976 fn check_pat(&mut self, cx: &EarlyContext<'_>, p: &ast::Pat) {
977 use ast::{Mutability, PatKind::*};
978 let keep_space = (false, false);
980 // Do not lint on `(..)` as that will result in the other arms being useless.
982 // The other cases do not contain sub-patterns.
983 | Wild | Rest | Lit(..) | MacCall(..) | Range(..) | Ident(.., None) | Path(..) => {},
984 // These are list-like patterns; parens can always be removed.
985 TupleStruct(_, _, ps) | Tuple(ps) | Slice(ps) | Or(ps) => for p in ps {
986 self.check_unused_parens_pat(cx, p, false, false, keep_space);
988 Struct(_, _, fps, _) => for f in fps {
989 self.check_unused_parens_pat(cx, &f.pat, false, false, keep_space);
991 // Avoid linting on `i @ (p0 | .. | pn)` and `box (p0 | .. | pn)`, #64106.
992 Ident(.., Some(p)) | Box(p) => self.check_unused_parens_pat(cx, p, true, false, keep_space),
993 // Avoid linting on `&(mut x)` as `&mut x` has a different meaning, #55342.
994 // Also avoid linting on `& mut? (p0 | .. | pn)`, #64106.
995 Ref(p, m) => self.check_unused_parens_pat(cx, p, true, *m == Mutability::Not, keep_space),
999 fn check_stmt(&mut self, cx: &EarlyContext<'_>, s: &ast::Stmt) {
1000 if let StmtKind::Local(ref local) = s.kind {
1001 self.check_unused_parens_pat(cx, &local.pat, true, false, (false, false));
1004 <Self as UnusedDelimLint>::check_stmt(self, cx, s)
1007 fn check_param(&mut self, cx: &EarlyContext<'_>, param: &ast::Param) {
1008 self.check_unused_parens_pat(cx, ¶m.pat, true, false, (false, false));
1011 fn check_arm(&mut self, cx: &EarlyContext<'_>, arm: &ast::Arm) {
1012 self.check_unused_parens_pat(cx, &arm.pat, false, false, (false, false));
1015 fn check_ty(&mut self, cx: &EarlyContext<'_>, ty: &ast::Ty) {
1016 if let ast::TyKind::Paren(r) = &ty.kind {
1018 ast::TyKind::TraitObject(..) => {}
1019 ast::TyKind::BareFn(b) if b.generic_params.len() > 0 => {}
1020 ast::TyKind::ImplTrait(_, bounds) if bounds.len() > 1 => {}
1021 ast::TyKind::Array(_, len) => {
1022 self.check_unused_delims_expr(
1025 UnusedDelimsCtx::ArrayLenExpr,
1032 let spans = if let Some(r) = r.span.find_ancestor_inside(ty.span) {
1033 Some((ty.span.with_hi(r.lo()), ty.span.with_lo(r.hi())))
1037 self.emit_unused_delims(cx, ty.span, spans, "type", (false, false));
1043 fn check_item(&mut self, cx: &EarlyContext<'_>, item: &ast::Item) {
1044 <Self as UnusedDelimLint>::check_item(self, cx, item)
1049 /// The `unused_braces` lint detects unnecessary braces around an
1064 /// The braces are not needed, and should be removed. This is the
1065 /// preferred style for writing these expressions.
1066 pub(super) UNUSED_BRACES,
1068 "unnecessary braces around an expression"
1071 declare_lint_pass!(UnusedBraces => [UNUSED_BRACES]);
1073 impl UnusedDelimLint for UnusedBraces {
1074 const DELIM_STR: &'static str = "braces";
1076 const LINT_EXPR_IN_PATTERN_MATCHING_CTX: bool = false;
1078 fn lint(&self) -> &'static Lint {
1082 fn check_unused_delims_expr(
1084 cx: &EarlyContext<'_>,
1086 ctx: UnusedDelimsCtx,
1087 followed_by_block: bool,
1088 left_pos: Option<BytePos>,
1089 right_pos: Option<BytePos>,
1092 ast::ExprKind::Block(ref inner, None)
1093 if inner.rules == ast::BlockCheckMode::Default =>
1095 // emit a warning under the following conditions:
1097 // - the block does not have a label
1098 // - the block is not `unsafe`
1099 // - the block contains exactly one expression (do not lint `{ expr; }`)
1100 // - `followed_by_block` is true and the internal expr may contain a `{`
1101 // - the block is not multiline (do not lint multiline match arms)
1105 // somewhat_long_expression
1110 // - the block has no attribute and was not created inside a macro
1111 // - if the block is an `anon_const`, the inner expr must be a literal
1112 // (do not lint `struct A<const N: usize>; let _: A<{ 2 + 3 }>;`)
1114 // FIXME(const_generics): handle paths when #67075 is fixed.
1115 if let [stmt] = inner.stmts.as_slice() {
1116 if let ast::StmtKind::Expr(ref expr) = stmt.kind {
1117 if !Self::is_expr_delims_necessary(expr, followed_by_block, false)
1118 && (ctx != UnusedDelimsCtx::AnonConst
1119 || matches!(expr.kind, ast::ExprKind::Lit(_)))
1120 && !cx.sess().source_map().is_multiline(value.span)
1121 && value.attrs.is_empty()
1122 && !value.span.from_expansion()
1124 self.emit_unused_delims_expr(cx, value, ctx, left_pos, right_pos)
1129 ast::ExprKind::Let(_, ref expr, _) => {
1130 self.check_unused_delims_expr(
1133 UnusedDelimsCtx::LetScrutineeExpr,
1144 impl EarlyLintPass for UnusedBraces {
1145 fn check_stmt(&mut self, cx: &EarlyContext<'_>, s: &ast::Stmt) {
1146 <Self as UnusedDelimLint>::check_stmt(self, cx, s)
1149 fn check_expr(&mut self, cx: &EarlyContext<'_>, e: &ast::Expr) {
1150 <Self as UnusedDelimLint>::check_expr(self, cx, e);
1152 if let ExprKind::Repeat(_, ref anon_const) = e.kind {
1153 self.check_unused_delims_expr(
1156 UnusedDelimsCtx::AnonConst,
1164 fn check_generic_arg(&mut self, cx: &EarlyContext<'_>, arg: &ast::GenericArg) {
1165 if let ast::GenericArg::Const(ct) = arg {
1166 self.check_unused_delims_expr(
1169 UnusedDelimsCtx::AnonConst,
1177 fn check_variant(&mut self, cx: &EarlyContext<'_>, v: &ast::Variant) {
1178 if let Some(anon_const) = &v.disr_expr {
1179 self.check_unused_delims_expr(
1182 UnusedDelimsCtx::AnonConst,
1190 fn check_ty(&mut self, cx: &EarlyContext<'_>, ty: &ast::Ty) {
1192 ast::TyKind::Array(_, ref len) => {
1193 self.check_unused_delims_expr(
1196 UnusedDelimsCtx::ArrayLenExpr,
1203 ast::TyKind::Typeof(ref anon_const) => {
1204 self.check_unused_delims_expr(
1207 UnusedDelimsCtx::AnonConst,
1218 fn check_item(&mut self, cx: &EarlyContext<'_>, item: &ast::Item) {
1219 <Self as UnusedDelimLint>::check_item(self, cx, item)
1224 /// The `unused_import_braces` lint catches unnecessary braces around an
1229 /// ```rust,compile_fail
1230 /// #![deny(unused_import_braces)]
1243 /// If there is only a single item, then remove the braces (`use test::A;`
1246 /// This lint is "allow" by default because it is only enforcing a
1247 /// stylistic choice.
1248 UNUSED_IMPORT_BRACES,
1250 "unnecessary braces around an imported item"
1253 declare_lint_pass!(UnusedImportBraces => [UNUSED_IMPORT_BRACES]);
1255 impl UnusedImportBraces {
1256 fn check_use_tree(&self, cx: &EarlyContext<'_>, use_tree: &ast::UseTree, item: &ast::Item) {
1257 if let ast::UseTreeKind::Nested(ref items) = use_tree.kind {
1258 // Recursively check nested UseTrees
1259 for &(ref tree, _) in items {
1260 self.check_use_tree(cx, tree, item);
1263 // Trigger the lint only if there is one nested item
1264 if items.len() != 1 {
1268 // Trigger the lint if the nested item is a non-self single item
1269 let node_name = match items[0].0.kind {
1270 ast::UseTreeKind::Simple(rename) => {
1271 let orig_ident = items[0].0.prefix.segments.last().unwrap().ident;
1272 if orig_ident.name == kw::SelfLower {
1275 rename.unwrap_or(orig_ident).name
1277 ast::UseTreeKind::Glob => Symbol::intern("*"),
1278 ast::UseTreeKind::Nested(_) => return,
1281 cx.struct_span_lint(
1282 UNUSED_IMPORT_BRACES,
1284 fluent::lint_unused_import_braces,
1285 |lint| lint.set_arg("node", node_name),
1291 impl EarlyLintPass for UnusedImportBraces {
1292 fn check_item(&mut self, cx: &EarlyContext<'_>, item: &ast::Item) {
1293 if let ast::ItemKind::Use(ref use_tree) = item.kind {
1294 self.check_use_tree(cx, use_tree, item);
1300 /// The `unused_allocation` lint detects unnecessary allocations that can
1306 /// #![feature(box_syntax)]
1308 /// let a = (box [1, 2, 3]).len();
1316 /// When a `box` expression is immediately coerced to a reference, then
1317 /// the allocation is unnecessary, and a reference (using `&` or `&mut`)
1318 /// should be used instead to avoid the allocation.
1319 pub(super) UNUSED_ALLOCATION,
1321 "detects unnecessary allocations that can be eliminated"
1324 declare_lint_pass!(UnusedAllocation => [UNUSED_ALLOCATION]);
1326 impl<'tcx> LateLintPass<'tcx> for UnusedAllocation {
1327 fn check_expr(&mut self, cx: &LateContext<'_>, e: &hir::Expr<'_>) {
1329 hir::ExprKind::Box(_) => {}
1333 for adj in cx.typeck_results().expr_adjustments(e) {
1334 if let adjustment::Adjust::Borrow(adjustment::AutoBorrow::Ref(_, m)) = adj.kind {
1335 cx.struct_span_lint(
1339 adjustment::AutoBorrowMutability::Not => fluent::lint_unused_allocation,
1340 adjustment::AutoBorrowMutability::Mut { .. } => {
1341 fluent::lint_unused_allocation_mut