Skip to main content

rustc_lint/
unused.rs

1use rustc_ast::util::{classify, parser};
2use rustc_ast::{self as ast, ExprKind, FnRetTy, HasAttrs as _, StmtKind};
3use rustc_data_structures::fx::FxHashMap;
4use rustc_errors::MultiSpan;
5use rustc_hir::{self as hir};
6use rustc_middle::ty::{self, adjustment};
7use rustc_session::{declare_lint, declare_lint_pass, impl_lint_pass};
8use rustc_span::edition::Edition::Edition2015;
9use rustc_span::{BytePos, Span, kw, sym};
10
11use crate::lints::{
12    PathStatementDrop, PathStatementDropSub, PathStatementNoEffect, UnusedAllocationDiag,
13    UnusedAllocationMutDiag, UnusedDelim, UnusedDelimSuggestion, UnusedImportBracesDiag,
14};
15use crate::{EarlyContext, EarlyLintPass, LateContext, LateLintPass, Lint, LintContext};
16
17pub mod must_use;
18
19#[doc =
r" The `path_statements` lint detects path statements with no effect."]
#[doc = r""]
#[doc = r" ### Example"]
#[doc = r""]
#[doc = r" ```rust"]
#[doc = r" let x = 42;"]
#[doc = r""]
#[doc = r" x;"]
#[doc = r" ```"]
#[doc = r""]
#[doc = r" {{produces}}"]
#[doc = r""]
#[doc = r" ### Explanation"]
#[doc = r""]
#[doc = r" It is usually a mistake to have a statement that has no effect."]
pub static PATH_STATEMENTS: &::rustc_lint_defs::Lint =
    &::rustc_lint_defs::Lint {
            name: "PATH_STATEMENTS",
            default_level: ::rustc_lint_defs::Warn,
            desc: "path statements with no effect",
            is_externally_loaded: false,
            ..::rustc_lint_defs::Lint::default_fields_for_macro()
        };declare_lint! {
20    /// The `path_statements` lint detects path statements with no effect.
21    ///
22    /// ### Example
23    ///
24    /// ```rust
25    /// let x = 42;
26    ///
27    /// x;
28    /// ```
29    ///
30    /// {{produces}}
31    ///
32    /// ### Explanation
33    ///
34    /// It is usually a mistake to have a statement that has no effect.
35    pub PATH_STATEMENTS,
36    Warn,
37    "path statements with no effect"
38}
39
40pub struct PathStatements;
#[automatically_derived]
impl ::core::marker::Copy for PathStatements { }
#[automatically_derived]
#[doc(hidden)]
unsafe impl ::core::clone::TrivialClone for PathStatements { }
#[automatically_derived]
impl ::core::clone::Clone for PathStatements {
    #[inline]
    fn clone(&self) -> PathStatements { *self }
}
impl ::rustc_lint_defs::LintPass for PathStatements {
    fn name(&self) -> &'static str { "PathStatements" }
    fn get_lints(&self) -> ::rustc_lint_defs::LintVec {
        ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
                [PATH_STATEMENTS]))
    }
}
impl PathStatements {
    #[allow(unused)]
    pub fn lint_vec() -> ::rustc_lint_defs::LintVec {
        ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
                [PATH_STATEMENTS]))
    }
}declare_lint_pass!(PathStatements => [PATH_STATEMENTS]);
41
42impl<'tcx> LateLintPass<'tcx> for PathStatements {
43    fn check_stmt(&mut self, cx: &LateContext<'_>, s: &hir::Stmt<'_>) {
44        if let hir::StmtKind::Semi(expr) = s.kind
45            && let hir::ExprKind::Path(_) = expr.kind
46        {
47            let ty = cx.typeck_results().expr_ty(expr);
48            if ty.needs_drop(cx.tcx, cx.typing_env()) {
49                let sub = if let Ok(snippet) = cx.sess().source_map().span_to_snippet(expr.span) {
50                    PathStatementDropSub::Suggestion { span: s.span, snippet }
51                } else {
52                    PathStatementDropSub::Help { span: s.span }
53                };
54                cx.emit_span_lint(PATH_STATEMENTS, s.span, PathStatementDrop { sub })
55            } else {
56                cx.emit_span_lint(PATH_STATEMENTS, s.span, PathStatementNoEffect);
57            }
58        }
59    }
60}
61
62#[derive(#[automatically_derived]
impl ::core::marker::Copy for UnusedDelimsCtx { }Copy, #[automatically_derived]
impl ::core::clone::Clone for UnusedDelimsCtx {
    #[inline]
    fn clone(&self) -> UnusedDelimsCtx { *self }
}Clone, #[automatically_derived]
impl ::core::fmt::Debug for UnusedDelimsCtx {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::write_str(f,
            match self {
                UnusedDelimsCtx::FunctionArg => "FunctionArg",
                UnusedDelimsCtx::MethodArg => "MethodArg",
                UnusedDelimsCtx::AssignedValue => "AssignedValue",
                UnusedDelimsCtx::AssignedValueLetElse =>
                    "AssignedValueLetElse",
                UnusedDelimsCtx::IfCond => "IfCond",
                UnusedDelimsCtx::WhileCond => "WhileCond",
                UnusedDelimsCtx::ForIterExpr => "ForIterExpr",
                UnusedDelimsCtx::MatchScrutineeExpr => "MatchScrutineeExpr",
                UnusedDelimsCtx::ReturnValue => "ReturnValue",
                UnusedDelimsCtx::BlockRetValue => "BlockRetValue",
                UnusedDelimsCtx::BreakValue => "BreakValue",
                UnusedDelimsCtx::LetScrutineeExpr => "LetScrutineeExpr",
                UnusedDelimsCtx::ArrayLenExpr => "ArrayLenExpr",
                UnusedDelimsCtx::AnonConst => "AnonConst",
                UnusedDelimsCtx::MatchArmExpr => "MatchArmExpr",
                UnusedDelimsCtx::IndexExpr => "IndexExpr",
                UnusedDelimsCtx::ClosureBody => "ClosureBody",
            })
    }
}Debug, #[automatically_derived]
impl ::core::cmp::PartialEq for UnusedDelimsCtx {
    #[inline]
    fn eq(&self, other: &UnusedDelimsCtx) -> bool {
        let __self_discr = ::core::intrinsics::discriminant_value(self);
        let __arg1_discr = ::core::intrinsics::discriminant_value(other);
        __self_discr == __arg1_discr
    }
}PartialEq, #[automatically_derived]
impl ::core::cmp::Eq for UnusedDelimsCtx {
    #[inline]
    #[doc(hidden)]
    #[coverage(off)]
    fn assert_fields_are_eq(&self) {}
}Eq)]
63enum UnusedDelimsCtx {
64    FunctionArg,
65    MethodArg,
66    AssignedValue,
67    AssignedValueLetElse,
68    IfCond,
69    WhileCond,
70    ForIterExpr,
71    MatchScrutineeExpr,
72    ReturnValue,
73    BlockRetValue,
74    BreakValue,
75    LetScrutineeExpr,
76    ArrayLenExpr,
77    AnonConst,
78    MatchArmExpr,
79    IndexExpr,
80    ClosureBody,
81}
82
83impl From<UnusedDelimsCtx> for &'static str {
84    fn from(ctx: UnusedDelimsCtx) -> &'static str {
85        match ctx {
86            UnusedDelimsCtx::FunctionArg => "function argument",
87            UnusedDelimsCtx::MethodArg => "method argument",
88            UnusedDelimsCtx::AssignedValue | UnusedDelimsCtx::AssignedValueLetElse => {
89                "assigned value"
90            }
91            UnusedDelimsCtx::IfCond => "`if` condition",
92            UnusedDelimsCtx::WhileCond => "`while` condition",
93            UnusedDelimsCtx::ForIterExpr => "`for` iterator expression",
94            UnusedDelimsCtx::MatchScrutineeExpr => "`match` scrutinee expression",
95            UnusedDelimsCtx::ReturnValue => "`return` value",
96            UnusedDelimsCtx::BlockRetValue => "block return value",
97            UnusedDelimsCtx::BreakValue => "`break` value",
98            UnusedDelimsCtx::LetScrutineeExpr => "`let` scrutinee expression",
99            UnusedDelimsCtx::ArrayLenExpr | UnusedDelimsCtx::AnonConst => "const expression",
100            UnusedDelimsCtx::MatchArmExpr => "match arm expression",
101            UnusedDelimsCtx::IndexExpr => "index expression",
102            UnusedDelimsCtx::ClosureBody => "closure body",
103        }
104    }
105}
106
107/// Used by both `UnusedParens` and `UnusedBraces` to prevent code duplication.
108trait UnusedDelimLint {
109    const DELIM_STR: &'static str;
110
111    /// Due to `ref` pattern, there can be a difference between using
112    /// `{ expr }` and `expr` in pattern-matching contexts. This means
113    /// that we should only lint `unused_parens` and not `unused_braces`
114    /// in this case.
115    ///
116    /// ```rust
117    /// let mut a = 7;
118    /// let ref b = { a }; // We actually borrow a copy of `a` here.
119    /// a += 1; // By mutating `a` we invalidate any borrows of `a`.
120    /// assert_eq!(b + 1, a); // `b` does not borrow `a`, so we can still use it here.
121    /// ```
122    const LINT_EXPR_IN_PATTERN_MATCHING_CTX: bool;
123
124    // this cannot be a constant is it refers to a static.
125    fn lint(&self) -> &'static Lint;
126
127    fn check_unused_delims_expr(
128        &self,
129        cx: &EarlyContext<'_>,
130        value: &ast::Expr,
131        ctx: UnusedDelimsCtx,
132        followed_by_block: bool,
133        left_pos: Option<BytePos>,
134        right_pos: Option<BytePos>,
135        is_kw: bool,
136    );
137
138    fn is_expr_delims_necessary(
139        inner: &ast::Expr,
140        ctx: UnusedDelimsCtx,
141        followed_by_block: bool,
142    ) -> bool {
143        let followed_by_else = ctx == UnusedDelimsCtx::AssignedValueLetElse;
144
145        if followed_by_else {
146            match inner.kind {
147                ast::ExprKind::Binary(op, ..) if op.node.is_lazy() => return true,
148                _ if classify::expr_trailing_brace(inner).is_some() => return true,
149                _ => {}
150            }
151        }
152
153        // Check it's range in LetScrutineeExpr
154        if let ast::ExprKind::Range(..) = inner.kind
155            && #[allow(non_exhaustive_omitted_patterns)] match ctx {
    UnusedDelimsCtx::LetScrutineeExpr => true,
    _ => false,
}matches!(ctx, UnusedDelimsCtx::LetScrutineeExpr)
156        {
157            return true;
158        }
159
160        // Do not lint against parentheses around `&raw [const|mut] expr`.
161        // These parentheses will have to be added e.g. when calling a method on the result of this
162        // expression, and we want to avoid churn wrt adding and removing parentheses.
163        if #[allow(non_exhaustive_omitted_patterns)] match inner.kind {
    ast::ExprKind::AddrOf(ast::BorrowKind::Raw, ..) => true,
    _ => false,
}matches!(inner.kind, ast::ExprKind::AddrOf(ast::BorrowKind::Raw, ..)) {
164            return true;
165        }
166
167        // Check if LHS needs parens to prevent false-positives in cases like
168        // `fn x() -> u8 { ({ 0 } + 1) }`.
169        //
170        // FIXME: https://github.com/rust-lang/rust/issues/119426
171        // The syntax tree in this code is from after macro expansion, so the
172        // current implementation has both false negatives and false positives
173        // related to expressions containing macros.
174        //
175        //     macro_rules! m1 {
176        //         () => {
177        //             1
178        //         };
179        //     }
180        //
181        //     fn f1() -> u8 {
182        //         // Lint says parens are not needed, but they are.
183        //         (m1! {} + 1)
184        //     }
185        //
186        //     macro_rules! m2 {
187        //         () => {
188        //             loop { break 1; }
189        //         };
190        //     }
191        //
192        //     fn f2() -> u8 {
193        //         // Lint says parens are needed, but they are not.
194        //         (m2!() + 1)
195        //     }
196        {
197            let mut innermost = inner;
198            loop {
199                innermost = match &innermost.kind {
200                    ExprKind::Binary(_op, lhs, _rhs) => lhs,
201                    ExprKind::Call(fn_, _params) => fn_,
202                    ExprKind::Cast(expr, _ty) => expr,
203                    ExprKind::Type(expr, _ty) => expr,
204                    ExprKind::Index(base, _subscript, _) => base,
205                    _ => break,
206                };
207                if !classify::expr_requires_semi_to_be_stmt(innermost) {
208                    return true;
209                }
210            }
211        }
212
213        // Check if RHS needs parens to prevent false-positives in cases like `if (() == return)
214        // {}`.
215        if !followed_by_block {
216            return false;
217        }
218
219        // Check if we need parens for `match &( Struct { field:  }) {}`.
220        {
221            let mut innermost = inner;
222            loop {
223                innermost = match &innermost.kind {
224                    ExprKind::AddrOf(_, _, expr) => expr,
225                    _ => {
226                        if parser::contains_exterior_struct_lit(innermost) {
227                            return true;
228                        } else {
229                            break;
230                        }
231                    }
232                }
233            }
234        }
235
236        let mut innermost = inner;
237        loop {
238            innermost = match &innermost.kind {
239                ExprKind::Unary(_op, expr) => expr,
240                ExprKind::Binary(_op, _lhs, rhs) => rhs,
241                ExprKind::AssignOp(_op, _lhs, rhs) => rhs,
242                ExprKind::Assign(_lhs, rhs, _span) => rhs,
243
244                ExprKind::Ret(_) | ExprKind::Yield(..) | ExprKind::Yeet(..) => return true,
245
246                ExprKind::Break(_label, None) => return false,
247                ExprKind::Break(_label, Some(break_expr)) => {
248                    // `if (break 'label i) { ... }` removing parens would make `i { ... }`
249                    // be parsed as a struct literal, so keep parentheses if the break value
250                    // ends with a path (which could be mistaken for a struct name).
251                    return #[allow(non_exhaustive_omitted_patterns)] match break_expr.kind {
    ExprKind::Block(..) | ExprKind::Path(..) => true,
    _ => false,
}matches!(break_expr.kind, ExprKind::Block(..) | ExprKind::Path(..));
252                }
253
254                ExprKind::Range(_lhs, Some(rhs), _limits) => {
255                    return #[allow(non_exhaustive_omitted_patterns)] match rhs.kind {
    ExprKind::Block(..) => true,
    _ => false,
}matches!(rhs.kind, ExprKind::Block(..));
256                }
257
258                _ => return parser::contains_exterior_struct_lit(inner),
259            }
260        }
261    }
262
263    fn emit_unused_delims_expr(
264        &self,
265        cx: &EarlyContext<'_>,
266        value: &ast::Expr,
267        ctx: UnusedDelimsCtx,
268        left_pos: Option<BytePos>,
269        right_pos: Option<BytePos>,
270        is_kw: bool,
271    ) {
272        let span_with_attrs = match value.kind {
273            ast::ExprKind::Block(ref block, None) if let [stmt] = block.stmts.as_slice() => {
274                // For the statements with attributes, like `{ #[allow()] println!("Hello!") }`,
275                // the span should contains the attributes, or the suggestion will remove them.
276                if let Some(attr_lo) = stmt.attrs().iter().map(|attr| attr.span.lo()).min() {
277                    stmt.span.with_lo(attr_lo)
278                } else {
279                    stmt.span
280                }
281            }
282            ast::ExprKind::Paren(ref expr) => {
283                // For the expr with attributes, like `let _ = (#[inline] || println!("Hello!"));`,
284                // the span should contains the attributes, or the suggestion will remove them.
285                if let Some(attr_lo) = expr.attrs.iter().map(|attr| attr.span.lo()).min() {
286                    expr.span.with_lo(attr_lo)
287                } else {
288                    expr.span
289                }
290            }
291            _ => return,
292        };
293        let spans = span_with_attrs
294            .find_ancestor_inside(value.span)
295            .map(|span| (value.span.with_hi(span.lo()), value.span.with_lo(span.hi())));
296        let keep_space = (
297            left_pos.is_some_and(|s| s >= value.span.lo()),
298            right_pos.is_some_and(|s| s <= value.span.hi()),
299        );
300        self.emit_unused_delims(cx, value.span, spans, ctx.into(), keep_space, is_kw);
301    }
302
303    fn emit_unused_delims(
304        &self,
305        cx: &EarlyContext<'_>,
306        value_span: Span,
307        spans: Option<(Span, Span)>,
308        msg: &str,
309        keep_space: (bool, bool),
310        is_kw: bool,
311    ) {
312        let primary_span = if let Some((lo, hi)) = spans {
313            if hi.is_empty() {
314                // do not point at delims that do not exist
315                return;
316            }
317            MultiSpan::from(::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [lo, hi]))vec![lo, hi])
318        } else {
319            MultiSpan::from(value_span)
320        };
321        let suggestion = spans.map(|(lo, hi)| {
322            let sm = cx.sess().source_map();
323            let lo_replace = if (keep_space.0 || is_kw)
324                && let Ok(snip) = sm.span_to_prev_source(lo)
325                && !snip.ends_with(' ')
326            {
327                " "
328            } else if let Ok(snip) = sm.span_to_prev_source(value_span)
329                && snip.ends_with(|c: char| c.is_alphanumeric())
330            {
331                " "
332            } else {
333                ""
334            };
335
336            let hi_replace = if keep_space.1
337                && let Ok(snip) = sm.span_to_next_source(hi)
338                && !snip.starts_with(' ')
339            {
340                " "
341            } else if let Ok(snip) = sm.span_to_prev_source(value_span)
342                && snip.starts_with(|c: char| c.is_alphanumeric())
343            {
344                " "
345            } else {
346                ""
347            };
348            UnusedDelimSuggestion {
349                start_span: lo,
350                start_replace: lo_replace,
351                end_span: hi,
352                end_replace: hi_replace,
353                delim: Self::DELIM_STR,
354            }
355        });
356        cx.emit_span_lint(
357            self.lint(),
358            primary_span,
359            UnusedDelim { delim: Self::DELIM_STR, item: msg, suggestion },
360        );
361    }
362
363    fn check_expr(&mut self, cx: &EarlyContext<'_>, e: &ast::Expr) {
364        use rustc_ast::ExprKind::*;
365        let (value, ctx, followed_by_block, left_pos, right_pos, is_kw) = match e.kind {
366            // Do not lint `unused_braces` in `if let` expressions.
367            If(ref cond, ref block, _)
368                if !#[allow(non_exhaustive_omitted_patterns)] match cond.kind {
    Let(..) => true,
    _ => false,
}matches!(cond.kind, Let(..)) || Self::LINT_EXPR_IN_PATTERN_MATCHING_CTX =>
369            {
370                let left = e.span.lo() + rustc_span::BytePos(2);
371                let right = block.span.lo();
372                (cond, UnusedDelimsCtx::IfCond, true, Some(left), Some(right), true)
373            }
374
375            // Do not lint `unused_braces` in `while let` expressions.
376            While(ref cond, ref block, ..)
377                if !#[allow(non_exhaustive_omitted_patterns)] match cond.kind {
    Let(..) => true,
    _ => false,
}matches!(cond.kind, Let(..)) || Self::LINT_EXPR_IN_PATTERN_MATCHING_CTX =>
378            {
379                let left = e.span.lo() + rustc_span::BytePos(5);
380                let right = block.span.lo();
381                (cond, UnusedDelimsCtx::WhileCond, true, Some(left), Some(right), true)
382            }
383
384            ForLoop { ref iter, ref body, .. } => {
385                (iter, UnusedDelimsCtx::ForIterExpr, true, None, Some(body.span.lo()), true)
386            }
387
388            Match(ref head, _, ast::MatchKind::Prefix)
389                if Self::LINT_EXPR_IN_PATTERN_MATCHING_CTX =>
390            {
391                let left = e.span.lo() + rustc_span::BytePos(5);
392                (head, UnusedDelimsCtx::MatchScrutineeExpr, true, Some(left), None, true)
393            }
394
395            Ret(Some(ref value)) => {
396                let left = e.span.lo() + rustc_span::BytePos(3);
397                (value, UnusedDelimsCtx::ReturnValue, false, Some(left), None, true)
398            }
399
400            Break(label, Some(ref value)) => {
401                // Don't lint on `break 'label ({...})` - the parens are necessary
402                // to disambiguate from `break 'label {...}` which would be a syntax error.
403                // This avoids conflicts with the `break_with_label_and_loop` lint.
404                if label.is_some()
405                    && #[allow(non_exhaustive_omitted_patterns)] match value.kind {
    ast::ExprKind::Paren(ref inner) if
        #[allow(non_exhaustive_omitted_patterns)] match inner.kind {
            ast::ExprKind::Block(..) => true,
            _ => false,
        } => true,
    _ => false,
}matches!(value.kind, ast::ExprKind::Paren(ref inner)
406                        if matches!(inner.kind, ast::ExprKind::Block(..)))
407                {
408                    return;
409                }
410                (value, UnusedDelimsCtx::BreakValue, false, None, None, true)
411            }
412
413            Index(_, ref value, _) => (value, UnusedDelimsCtx::IndexExpr, false, None, None, false),
414
415            Assign(_, ref value, _) | AssignOp(.., ref value) => {
416                (value, UnusedDelimsCtx::AssignedValue, false, None, None, false)
417            }
418            // either function/method call, or something this lint doesn't care about
419            ref call_or_other => {
420                let (args_to_check, ctx) = match *call_or_other {
421                    Call(_, ref args) => (&args[..], UnusedDelimsCtx::FunctionArg),
422                    MethodCall(ref call) => (&call.args[..], UnusedDelimsCtx::MethodArg),
423                    Closure(ref closure)
424                        if #[allow(non_exhaustive_omitted_patterns)] match closure.fn_decl.output {
    FnRetTy::Default(_) => true,
    _ => false,
}matches!(closure.fn_decl.output, FnRetTy::Default(_)) =>
425                    {
426                        (&[closure.body.clone()][..], UnusedDelimsCtx::ClosureBody)
427                    }
428                    // actual catch-all arm
429                    _ => {
430                        return;
431                    }
432                };
433                // Don't lint if this is a nested macro expansion: otherwise, the lint could
434                // trigger in situations that macro authors shouldn't have to care about, e.g.,
435                // when a parenthesized token tree matched in one macro expansion is matched as
436                // an expression in another and used as a fn/method argument (Issue #47775)
437                if e.span.ctxt().outer_expn_data().call_site.from_expansion() {
438                    return;
439                }
440                for arg in args_to_check {
441                    self.check_unused_delims_expr(cx, arg, ctx, false, None, None, false);
442                }
443                return;
444            }
445        };
446        self.check_unused_delims_expr(
447            cx,
448            value,
449            ctx,
450            followed_by_block,
451            left_pos,
452            right_pos,
453            is_kw,
454        );
455    }
456
457    fn check_stmt(&mut self, cx: &EarlyContext<'_>, s: &ast::Stmt) {
458        match s.kind {
459            StmtKind::Let(ref local) if Self::LINT_EXPR_IN_PATTERN_MATCHING_CTX => {
460                if let Some((init, els)) = local.kind.init_else_opt() {
461                    if els.is_some()
462                        && let ExprKind::Paren(paren) = &init.kind
463                        && !init.span.eq_ctxt(paren.span)
464                    {
465                        // This branch prevents cases where parentheses wrap an expression
466                        // resulting from macro expansion, such as:
467                        // ```
468                        // macro_rules! x {
469                        // () => { None::<i32> };
470                        // }
471                        // let Some(_) = (x!{}) else { return };
472                        // // -> let Some(_) = (None::<i32>) else { return };
473                        // //                  ~           ~ No Lint
474                        // ```
475                        return;
476                    }
477                    let ctx = match els {
478                        None => UnusedDelimsCtx::AssignedValue,
479                        Some(_) => UnusedDelimsCtx::AssignedValueLetElse,
480                    };
481                    self.check_unused_delims_expr(cx, init, ctx, false, None, None, false);
482                }
483            }
484            StmtKind::Expr(ref expr) => {
485                self.check_unused_delims_expr(
486                    cx,
487                    expr,
488                    UnusedDelimsCtx::BlockRetValue,
489                    false,
490                    None,
491                    None,
492                    false,
493                );
494            }
495            _ => {}
496        }
497    }
498
499    fn check_item(&mut self, cx: &EarlyContext<'_>, item: &ast::Item) {
500        use ast::ItemKind::*;
501
502        let expr = if let Const(box ast::ConstItem { rhs_kind, .. }) = &item.kind {
503            if let Some(e) = rhs_kind.expr() { e } else { return }
504        } else if let Static(box ast::StaticItem { expr: Some(expr), .. }) = &item.kind {
505            expr
506        } else {
507            return;
508        };
509        self.check_unused_delims_expr(
510            cx,
511            expr,
512            UnusedDelimsCtx::AssignedValue,
513            false,
514            None,
515            None,
516            false,
517        );
518    }
519}
520
521#[doc =
r" The `unused_parens` lint detects `if`, `match`, `while` and `return`"]
#[doc = r" with parentheses; they do not need them."]
#[doc = r""]
#[doc = r" ### Examples"]
#[doc = r""]
#[doc = r" ```rust"]
#[doc = r" if(true) {}"]
#[doc = r" ```"]
#[doc = r""]
#[doc = r" {{produces}}"]
#[doc = r""]
#[doc = r" ### Explanation"]
#[doc = r""]
#[doc =
r" The parentheses are not needed, and should be removed. This is the"]
#[doc = r" preferred style for writing these expressions."]
pub(super) static UNUSED_PARENS: &::rustc_lint_defs::Lint =
    &::rustc_lint_defs::Lint {
            name: "UNUSED_PARENS",
            default_level: ::rustc_lint_defs::Warn,
            desc: "`if`, `match`, `while` and `return` do not need parentheses",
            is_externally_loaded: false,
            ..::rustc_lint_defs::Lint::default_fields_for_macro()
        };declare_lint! {
522    /// The `unused_parens` lint detects `if`, `match`, `while` and `return`
523    /// with parentheses; they do not need them.
524    ///
525    /// ### Examples
526    ///
527    /// ```rust
528    /// if(true) {}
529    /// ```
530    ///
531    /// {{produces}}
532    ///
533    /// ### Explanation
534    ///
535    /// The parentheses are not needed, and should be removed. This is the
536    /// preferred style for writing these expressions.
537    pub(super) UNUSED_PARENS,
538    Warn,
539    "`if`, `match`, `while` and `return` do not need parentheses"
540}
541
542#[derive(#[automatically_derived]
impl ::core::default::Default for UnusedParens {
    #[inline]
    fn default() -> UnusedParens {
        UnusedParens {
            with_self_ty_parens: ::core::default::Default::default(),
            parens_in_cast_in_lt: ::core::default::Default::default(),
            in_no_bounds_pos: ::core::default::Default::default(),
        }
    }
}Default)]
543pub(crate) struct UnusedParens {
544    with_self_ty_parens: bool,
545    /// `1 as (i32) < 2` parses to ExprKind::Lt
546    /// `1 as i32 < 2` parses to i32::<2[missing angle bracket]
547    parens_in_cast_in_lt: Vec<ast::NodeId>,
548    /// Ty nodes in this map are in TypeNoBounds position. Any bounds they
549    /// contain may be ambiguous w/r/t trailing `+` operators.
550    in_no_bounds_pos: FxHashMap<ast::NodeId, NoBoundsException>,
551}
552
553/// Whether parentheses may be omitted from a type without resulting in ambiguity.
554///
555/// ```
556/// type Example = Box<dyn Fn() -> &'static (dyn Send) + Sync>;
557/// ```
558///
559/// Here, `&'static (dyn Send) + Sync` is a `TypeNoBounds`. As such, it may not directly
560/// contain `ImplTraitType` or `TraitObjectType` which is why `(dyn Send)` is parenthesized.
561/// However, an exception is made for `ImplTraitTypeOneBound` and `TraitObjectTypeOneBound`.
562/// The following is accepted because there is no `+`.
563///
564/// ```
565/// type Example = Box<dyn Fn() -> &'static dyn Send>;
566/// ```
567enum NoBoundsException {
568    /// The type must be parenthesized.
569    None,
570    /// The type is the last bound of the containing type expression. If it has exactly one bound,
571    /// parentheses around the type are unnecessary.
572    OneBound,
573}
574
575impl ::rustc_lint_defs::LintPass for UnusedParens {
    fn name(&self) -> &'static str { "UnusedParens" }
    fn get_lints(&self) -> ::rustc_lint_defs::LintVec {
        ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
                [UNUSED_PARENS]))
    }
}
impl UnusedParens {
    #[allow(unused)]
    pub fn lint_vec() -> ::rustc_lint_defs::LintVec {
        ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
                [UNUSED_PARENS]))
    }
}impl_lint_pass!(UnusedParens => [UNUSED_PARENS]);
576
577impl UnusedDelimLint for UnusedParens {
578    const DELIM_STR: &'static str = "parentheses";
579
580    const LINT_EXPR_IN_PATTERN_MATCHING_CTX: bool = true;
581
582    fn lint(&self) -> &'static Lint {
583        UNUSED_PARENS
584    }
585
586    fn check_unused_delims_expr(
587        &self,
588        cx: &EarlyContext<'_>,
589        value: &ast::Expr,
590        ctx: UnusedDelimsCtx,
591        followed_by_block: bool,
592        left_pos: Option<BytePos>,
593        right_pos: Option<BytePos>,
594        is_kw: bool,
595    ) {
596        match value.kind {
597            ast::ExprKind::Paren(ref inner) => {
598                if !Self::is_expr_delims_necessary(inner, ctx, followed_by_block)
599                    && value.attrs.is_empty()
600                    && !value.span.from_expansion()
601                    && (ctx != UnusedDelimsCtx::LetScrutineeExpr
602                        || !#[allow(non_exhaustive_omitted_patterns)] match inner.kind {
    ast::ExprKind::Binary(rustc_span::Spanned { node, .. }, _, _) if
        node.is_lazy() => true,
    _ => false,
}matches!(inner.kind, ast::ExprKind::Binary(
603                                rustc_span::Spanned { node, .. },
604                                _,
605                                _,
606                            ) if node.is_lazy()))
607                    && !((ctx == UnusedDelimsCtx::ReturnValue
608                        || ctx == UnusedDelimsCtx::BreakValue)
609                        && #[allow(non_exhaustive_omitted_patterns)] match inner.kind {
    ast::ExprKind::Assign(_, _, _) => true,
    _ => false,
}matches!(inner.kind, ast::ExprKind::Assign(_, _, _)))
610                {
611                    self.emit_unused_delims_expr(cx, value, ctx, left_pos, right_pos, is_kw)
612                }
613            }
614            ast::ExprKind::Let(_, ref expr, _, _) => {
615                self.check_unused_delims_expr(
616                    cx,
617                    expr,
618                    UnusedDelimsCtx::LetScrutineeExpr,
619                    followed_by_block,
620                    None,
621                    None,
622                    false,
623                );
624            }
625            _ => {}
626        }
627    }
628}
629
630impl UnusedParens {
631    fn check_unused_parens_pat(
632        &self,
633        cx: &EarlyContext<'_>,
634        value: &ast::Pat,
635        avoid_or: bool,
636        avoid_mut: bool,
637        keep_space: (bool, bool),
638    ) {
639        use ast::{BindingMode, PatKind};
640
641        if let PatKind::Paren(inner) = &value.kind {
642            match inner.kind {
643                // The lint visitor will visit each subpattern of `p`. We do not want to lint
644                // any range pattern no matter where it occurs in the pattern. For something like
645                // `&(a..=b)`, there is a recursive `check_pat` on `a` and `b`, but we will assume
646                // that if there are unnecessary parens they serve a purpose of readability.
647                PatKind::Range(..) => return,
648                // Parentheses may be necessary to disambiguate precedence in guard patterns.
649                PatKind::Guard(..) => return,
650                // Avoid `p0 | .. | pn` if we should.
651                PatKind::Or(..) if avoid_or => return,
652                // Avoid `mut x` and `mut x @ p` if we should:
653                PatKind::Ident(BindingMode::MUT, ..) if avoid_mut => {
654                    return;
655                }
656                // Otherwise proceed with linting.
657                _ => {}
658            }
659            let spans = if !value.span.from_expansion() {
660                inner
661                    .span
662                    .find_ancestor_inside(value.span)
663                    .map(|inner| (value.span.with_hi(inner.lo()), value.span.with_lo(inner.hi())))
664            } else {
665                None
666            };
667            self.emit_unused_delims(cx, value.span, spans, "pattern", keep_space, false);
668        }
669    }
670
671    fn cast_followed_by_lt(&self, expr: &ast::Expr) -> Option<ast::NodeId> {
672        if let ExprKind::Binary(op, lhs, _rhs) = &expr.kind
673            && (op.node == ast::BinOpKind::Lt || op.node == ast::BinOpKind::Shl)
674        {
675            let mut cur = lhs;
676            while let ExprKind::Binary(_, _, rhs) = &cur.kind {
677                cur = rhs;
678            }
679
680            if let ExprKind::Cast(_, ty) = &cur.kind
681                && let ast::TyKind::Paren(_) = &ty.kind
682            {
683                return Some(ty.id);
684            }
685        }
686        None
687    }
688}
689
690impl EarlyLintPass for UnusedParens {
691    #[inline]
692    fn check_expr(&mut self, cx: &EarlyContext<'_>, e: &ast::Expr) {
693        if let Some(ty_id) = self.cast_followed_by_lt(e) {
694            self.parens_in_cast_in_lt.push(ty_id);
695        }
696
697        match e.kind {
698            ExprKind::Let(ref pat, _, _, _) | ExprKind::ForLoop { ref pat, .. } => {
699                self.check_unused_parens_pat(cx, pat, false, false, (true, true));
700            }
701            // We ignore parens in cases like `if (((let Some(0) = Some(1))))` because we already
702            // handle a hard error for them during AST lowering in `lower_expr_mut`, but we still
703            // want to complain about things like `if let 42 = (42)`.
704            ExprKind::If(ref cond, ref block, ref else_)
705                if #[allow(non_exhaustive_omitted_patterns)] match cond.peel_parens().kind {
    ExprKind::Let(..) => true,
    _ => false,
}matches!(cond.peel_parens().kind, ExprKind::Let(..)) =>
706            {
707                self.check_unused_delims_expr(
708                    cx,
709                    cond.peel_parens(),
710                    UnusedDelimsCtx::LetScrutineeExpr,
711                    true,
712                    None,
713                    None,
714                    true,
715                );
716                for stmt in &block.stmts {
717                    <Self as UnusedDelimLint>::check_stmt(self, cx, stmt);
718                }
719                if let Some(e) = else_ {
720                    <Self as UnusedDelimLint>::check_expr(self, cx, e);
721                }
722                return;
723            }
724            ExprKind::Match(ref _expr, ref arm, _) => {
725                for a in arm {
726                    if let Some(body) = &a.body {
727                        self.check_unused_delims_expr(
728                            cx,
729                            body,
730                            UnusedDelimsCtx::MatchArmExpr,
731                            false,
732                            None,
733                            None,
734                            true,
735                        );
736                    }
737                }
738            }
739            _ => {}
740        }
741
742        <Self as UnusedDelimLint>::check_expr(self, cx, e)
743    }
744
745    fn check_expr_post(&mut self, _cx: &EarlyContext<'_>, e: &ast::Expr) {
746        if let Some(ty_id) = self.cast_followed_by_lt(e) {
747            let id = self
748                .parens_in_cast_in_lt
749                .pop()
750                .expect("check_expr and check_expr_post must balance");
751            match (&id, &ty_id) {
    (left_val, right_val) => {
        if !(*left_val == *right_val) {
            let kind = ::core::panicking::AssertKind::Eq;
            ::core::panicking::assert_failed(kind, &*left_val, &*right_val,
                ::core::option::Option::Some(format_args!("check_expr, check_ty, and check_expr_post are called, in that order, by the visitor")));
        }
    }
};assert_eq!(
752                id, ty_id,
753                "check_expr, check_ty, and check_expr_post are called, in that order, by the visitor"
754            );
755        }
756    }
757
758    fn check_pat(&mut self, cx: &EarlyContext<'_>, p: &ast::Pat) {
759        use ast::Mutability;
760        use ast::PatKind::*;
761        let keep_space = (false, false);
762        match &p.kind {
763            // Do not lint on `(..)` as that will result in the other arms being useless.
764            Paren(_)
765            // The other cases do not contain sub-patterns.
766            | Missing | Wild | Never | Rest | Expr(..) | MacCall(..) | Range(..) | Ident(.., None)
767            | Path(..) | Err(_) => {},
768            // These are list-like patterns; parens can always be removed.
769            TupleStruct(_, _, ps) | Tuple(ps) | Slice(ps) | Or(ps) => for p in ps {
770                self.check_unused_parens_pat(cx, p, false, false, keep_space);
771            },
772            Struct(_, _, fps, _) => for f in fps {
773                self.check_unused_parens_pat(cx, &f.pat, false, false, keep_space);
774            },
775            // Avoid linting on `i @ (p0 | .. | pn)` and `box (p0 | .. | pn)`, #64106.
776            Ident(.., Some(p)) | Box(p) | Deref(p) | Guard(p, _) => self.check_unused_parens_pat(cx, p, true, false, keep_space),
777            // Avoid linting on `&(mut x)` as `&mut x` has a different meaning, #55342.
778            // Also avoid linting on `& mut? (p0 | .. | pn)`, #64106.
779            // FIXME(pin_ergonomics): check pinned patterns
780            Ref(p, _, m) => self.check_unused_parens_pat(cx, p, true, *m == Mutability::Not, keep_space),
781        }
782    }
783
784    fn check_stmt(&mut self, cx: &EarlyContext<'_>, s: &ast::Stmt) {
785        if let StmtKind::Let(ref local) = s.kind {
786            self.check_unused_parens_pat(cx, &local.pat, true, false, (true, false));
787        }
788
789        <Self as UnusedDelimLint>::check_stmt(self, cx, s)
790    }
791
792    fn check_param(&mut self, cx: &EarlyContext<'_>, param: &ast::Param) {
793        self.check_unused_parens_pat(cx, &param.pat, true, false, (false, false));
794    }
795
796    fn check_arm(&mut self, cx: &EarlyContext<'_>, arm: &ast::Arm) {
797        self.check_unused_parens_pat(cx, &arm.pat, false, false, (false, false));
798    }
799
800    fn check_ty(&mut self, cx: &EarlyContext<'_>, ty: &ast::Ty) {
801        if let ast::TyKind::Paren(_) = ty.kind
802            && Some(&ty.id) == self.parens_in_cast_in_lt.last()
803        {
804            return;
805        }
806        match &ty.kind {
807            ast::TyKind::Array(_, len) => {
808                self.check_unused_delims_expr(
809                    cx,
810                    &len.value,
811                    UnusedDelimsCtx::ArrayLenExpr,
812                    false,
813                    None,
814                    None,
815                    false,
816                );
817            }
818            ast::TyKind::Paren(r) => {
819                let unused_parens = match &r.kind {
820                    ast::TyKind::ImplTrait(_, bounds) | ast::TyKind::TraitObject(bounds, _) => {
821                        match self.in_no_bounds_pos.get(&ty.id) {
822                            Some(NoBoundsException::None) => false,
823                            Some(NoBoundsException::OneBound) => bounds.len() <= 1,
824                            None => true,
825                        }
826                    }
827                    ast::TyKind::FnPtr(b) => {
828                        !self.with_self_ty_parens || b.generic_params.is_empty()
829                    }
830                    _ => true,
831                };
832
833                if unused_parens {
834                    let spans = (!ty.span.from_expansion())
835                        .then(|| {
836                            r.span
837                                .find_ancestor_inside(ty.span)
838                                .map(|r| (ty.span.with_hi(r.lo()), ty.span.with_lo(r.hi())))
839                        })
840                        .flatten();
841
842                    self.emit_unused_delims(cx, ty.span, spans, "type", (false, false), false);
843                }
844
845                self.with_self_ty_parens = false;
846            }
847            ast::TyKind::Ref(_, mut_ty) | ast::TyKind::Ptr(mut_ty) => {
848                // If this type itself appears in no-bounds position, we propagate its
849                // potentially tighter constraint or risk a false posive (issue 143653).
850                let own_constraint = self.in_no_bounds_pos.get(&ty.id);
851                let constraint = match own_constraint {
852                    Some(NoBoundsException::None) => NoBoundsException::None,
853                    Some(NoBoundsException::OneBound) => NoBoundsException::OneBound,
854                    None => NoBoundsException::OneBound,
855                };
856                self.in_no_bounds_pos.insert(mut_ty.ty.id, constraint);
857            }
858            ast::TyKind::TraitObject(bounds, _) | ast::TyKind::ImplTrait(_, bounds) => {
859                for i in 0..bounds.len() {
860                    let is_last = i == bounds.len() - 1;
861
862                    if let ast::GenericBound::Trait(poly_trait_ref) = &bounds[i] {
863                        let fn_with_explicit_ret_ty = if let [.., segment] =
864                            &*poly_trait_ref.trait_ref.path.segments
865                            && let Some(args) = segment.args.as_ref()
866                            && let ast::GenericArgs::Parenthesized(paren_args) = &**args
867                            && let ast::FnRetTy::Ty(ret_ty) = &paren_args.output
868                        {
869                            self.in_no_bounds_pos.insert(
870                                ret_ty.id,
871                                if is_last {
872                                    NoBoundsException::OneBound
873                                } else {
874                                    NoBoundsException::None
875                                },
876                            );
877
878                            true
879                        } else {
880                            false
881                        };
882
883                        // In edition 2015, dyn is a contextual keyword and `dyn::foo::Bar` is
884                        // parsed as a path, so parens are necessary to disambiguate. See
885                        //  - tests/ui/lint/unused/unused-parens-trait-obj-e2015.rs and
886                        //  - https://doc.rust-lang.org/reference/types/trait-object.html#r-type.trait-object.syntax-edition2018
887                        let dyn2015_exception = cx.sess().psess.edition == Edition2015
888                            && #[allow(non_exhaustive_omitted_patterns)] match ty.kind {
    ast::TyKind::TraitObject(..) => true,
    _ => false,
}matches!(ty.kind, ast::TyKind::TraitObject(..))
889                            && i == 0
890                            && poly_trait_ref
891                                .trait_ref
892                                .path
893                                .segments
894                                .first()
895                                .map(|s| s.ident.name == kw::PathRoot)
896                                .unwrap_or(false);
897
898                        if let ast::Parens::Yes = poly_trait_ref.parens
899                            && (is_last || !fn_with_explicit_ret_ty)
900                            && !dyn2015_exception
901                        {
902                            let s = poly_trait_ref.span;
903                            let spans = (!s.from_expansion()).then(|| {
904                                (
905                                    s.with_hi(s.lo() + rustc_span::BytePos(1)),
906                                    s.with_lo(s.hi() - rustc_span::BytePos(1)),
907                                )
908                            });
909
910                            self.emit_unused_delims(
911                                cx,
912                                poly_trait_ref.span,
913                                spans,
914                                "type",
915                                (false, false),
916                                false,
917                            );
918                        }
919                    }
920                }
921            }
922            _ => {}
923        }
924    }
925
926    fn check_item(&mut self, cx: &EarlyContext<'_>, item: &ast::Item) {
927        <Self as UnusedDelimLint>::check_item(self, cx, item)
928    }
929
930    fn check_item_post(&mut self, _: &EarlyContext<'_>, _: &rustc_ast::Item) {
931        self.in_no_bounds_pos.clear();
932    }
933
934    fn enter_where_predicate(&mut self, _: &EarlyContext<'_>, pred: &ast::WherePredicate) {
935        use rustc_ast::{WhereBoundPredicate, WherePredicateKind};
936        if let WherePredicateKind::BoundPredicate(WhereBoundPredicate {
937            bounded_ty,
938            bound_generic_params,
939            ..
940        }) = &pred.kind
941            && let ast::TyKind::Paren(_) = &bounded_ty.kind
942            && bound_generic_params.is_empty()
943        {
944            self.with_self_ty_parens = true;
945        }
946    }
947
948    fn exit_where_predicate(&mut self, _: &EarlyContext<'_>, _: &ast::WherePredicate) {
949        if !!self.with_self_ty_parens {
    ::core::panicking::panic("assertion failed: !self.with_self_ty_parens")
};assert!(!self.with_self_ty_parens);
950    }
951}
952
953#[doc = r" The `unused_braces` lint detects unnecessary braces around an"]
#[doc = r" expression."]
#[doc = r""]
#[doc = r" ### Example"]
#[doc = r""]
#[doc = r" ```rust"]
#[doc = r" if { true } {"]
#[doc = r"     // ..."]
#[doc = r" }"]
#[doc = r" ```"]
#[doc = r""]
#[doc = r" {{produces}}"]
#[doc = r""]
#[doc = r" ### Explanation"]
#[doc = r""]
#[doc = r" The braces are not needed, and should be removed. This is the"]
#[doc = r" preferred style for writing these expressions."]
pub(super) static UNUSED_BRACES: &::rustc_lint_defs::Lint =
    &::rustc_lint_defs::Lint {
            name: "UNUSED_BRACES",
            default_level: ::rustc_lint_defs::Warn,
            desc: "unnecessary braces around an expression",
            is_externally_loaded: false,
            ..::rustc_lint_defs::Lint::default_fields_for_macro()
        };declare_lint! {
954    /// The `unused_braces` lint detects unnecessary braces around an
955    /// expression.
956    ///
957    /// ### Example
958    ///
959    /// ```rust
960    /// if { true } {
961    ///     // ...
962    /// }
963    /// ```
964    ///
965    /// {{produces}}
966    ///
967    /// ### Explanation
968    ///
969    /// The braces are not needed, and should be removed. This is the
970    /// preferred style for writing these expressions.
971    pub(super) UNUSED_BRACES,
972    Warn,
973    "unnecessary braces around an expression"
974}
975
976pub struct UnusedBraces;
#[automatically_derived]
impl ::core::marker::Copy for UnusedBraces { }
#[automatically_derived]
#[doc(hidden)]
unsafe impl ::core::clone::TrivialClone for UnusedBraces { }
#[automatically_derived]
impl ::core::clone::Clone for UnusedBraces {
    #[inline]
    fn clone(&self) -> UnusedBraces { *self }
}
impl ::rustc_lint_defs::LintPass for UnusedBraces {
    fn name(&self) -> &'static str { "UnusedBraces" }
    fn get_lints(&self) -> ::rustc_lint_defs::LintVec {
        ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
                [UNUSED_BRACES]))
    }
}
impl UnusedBraces {
    #[allow(unused)]
    pub fn lint_vec() -> ::rustc_lint_defs::LintVec {
        ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
                [UNUSED_BRACES]))
    }
}declare_lint_pass!(UnusedBraces => [UNUSED_BRACES]);
977
978impl UnusedDelimLint for UnusedBraces {
979    const DELIM_STR: &'static str = "braces";
980
981    const LINT_EXPR_IN_PATTERN_MATCHING_CTX: bool = false;
982
983    fn lint(&self) -> &'static Lint {
984        UNUSED_BRACES
985    }
986
987    fn check_unused_delims_expr(
988        &self,
989        cx: &EarlyContext<'_>,
990        value: &ast::Expr,
991        ctx: UnusedDelimsCtx,
992        followed_by_block: bool,
993        left_pos: Option<BytePos>,
994        right_pos: Option<BytePos>,
995        is_kw: bool,
996    ) {
997        match value.kind {
998            ast::ExprKind::Block(ref inner, None)
999                if inner.rules == ast::BlockCheckMode::Default =>
1000            {
1001                // emit a warning under the following conditions:
1002                //
1003                // - the block does not have a label
1004                // - the block is not `unsafe`
1005                // - the block contains exactly one expression (do not lint `{ expr; }`)
1006                // - `followed_by_block` is true and the internal expr may contain a `{`
1007                // - the block is not multiline (do not lint multiline match arms)
1008                //      ```
1009                //      match expr {
1010                //          Pattern => {
1011                //              somewhat_long_expression
1012                //          }
1013                //          // ...
1014                //      }
1015                //      ```
1016                // - the block has no attribute and was not created inside a macro
1017                // - if the block is an `anon_const`, the inner expr must be a literal
1018                //   not created by a macro, i.e. do not lint on:
1019                //      ```
1020                //      struct A<const N: usize>;
1021                //      let _: A<{ 2 + 3 }>;
1022                //      let _: A<{produces_literal!()}>;
1023                //      ```
1024                // FIXME(const_generics): handle paths when #67075 is fixed.
1025                if let [stmt] = inner.stmts.as_slice()
1026                    && let ast::StmtKind::Expr(ref expr) = stmt.kind
1027                    && !Self::is_expr_delims_necessary(expr, ctx, followed_by_block)
1028                    && (ctx != UnusedDelimsCtx::AnonConst
1029                        || (#[allow(non_exhaustive_omitted_patterns)] match expr.kind {
    ast::ExprKind::Lit(_) => true,
    _ => false,
}matches!(expr.kind, ast::ExprKind::Lit(_))
1030                            && !expr.span.from_expansion()))
1031                    && ctx != UnusedDelimsCtx::ClosureBody
1032                    && !cx.sess().source_map().is_multiline(value.span)
1033                    && value.attrs.is_empty()
1034                    && !value.span.from_expansion()
1035                    && !inner.span.from_expansion()
1036                {
1037                    self.emit_unused_delims_expr(cx, value, ctx, left_pos, right_pos, is_kw)
1038                }
1039            }
1040            ast::ExprKind::Let(_, ref expr, _, _) => {
1041                self.check_unused_delims_expr(
1042                    cx,
1043                    expr,
1044                    UnusedDelimsCtx::LetScrutineeExpr,
1045                    followed_by_block,
1046                    None,
1047                    None,
1048                    false,
1049                );
1050            }
1051            _ => {}
1052        }
1053    }
1054}
1055
1056impl EarlyLintPass for UnusedBraces {
1057    fn check_stmt(&mut self, cx: &EarlyContext<'_>, s: &ast::Stmt) {
1058        <Self as UnusedDelimLint>::check_stmt(self, cx, s)
1059    }
1060
1061    #[inline]
1062    fn check_expr(&mut self, cx: &EarlyContext<'_>, e: &ast::Expr) {
1063        <Self as UnusedDelimLint>::check_expr(self, cx, e);
1064
1065        if let ExprKind::Repeat(_, ref anon_const) = e.kind {
1066            self.check_unused_delims_expr(
1067                cx,
1068                &anon_const.value,
1069                UnusedDelimsCtx::AnonConst,
1070                false,
1071                None,
1072                None,
1073                false,
1074            );
1075        }
1076    }
1077
1078    fn check_generic_arg(&mut self, cx: &EarlyContext<'_>, arg: &ast::GenericArg) {
1079        if let ast::GenericArg::Const(ct) = arg {
1080            self.check_unused_delims_expr(
1081                cx,
1082                &ct.value,
1083                UnusedDelimsCtx::AnonConst,
1084                false,
1085                None,
1086                None,
1087                false,
1088            );
1089        }
1090    }
1091
1092    fn check_variant(&mut self, cx: &EarlyContext<'_>, v: &ast::Variant) {
1093        if let Some(anon_const) = &v.disr_expr {
1094            self.check_unused_delims_expr(
1095                cx,
1096                &anon_const.value,
1097                UnusedDelimsCtx::AnonConst,
1098                false,
1099                None,
1100                None,
1101                false,
1102            );
1103        }
1104    }
1105
1106    fn check_ty(&mut self, cx: &EarlyContext<'_>, ty: &ast::Ty) {
1107        match ty.kind {
1108            ast::TyKind::Array(_, ref len) => {
1109                self.check_unused_delims_expr(
1110                    cx,
1111                    &len.value,
1112                    UnusedDelimsCtx::ArrayLenExpr,
1113                    false,
1114                    None,
1115                    None,
1116                    false,
1117                );
1118            }
1119
1120            _ => {}
1121        }
1122    }
1123
1124    fn check_item(&mut self, cx: &EarlyContext<'_>, item: &ast::Item) {
1125        <Self as UnusedDelimLint>::check_item(self, cx, item)
1126    }
1127}
1128
1129#[doc =
r" The `unused_import_braces` lint catches unnecessary braces around an"]
#[doc = r" imported item."]
#[doc = r""]
#[doc = r" ### Example"]
#[doc = r""]
#[doc = r" ```rust,compile_fail"]
#[doc = r" #![deny(unused_import_braces)]"]
#[doc = r" use test::{A};"]
#[doc = r""]
#[doc = r" pub mod test {"]
#[doc = r"     pub struct A;"]
#[doc = r" }"]
#[doc = r" # fn main() {}"]
#[doc = r" ```"]
#[doc = r""]
#[doc = r" {{produces}}"]
#[doc = r""]
#[doc = r" ### Explanation"]
#[doc = r""]
#[doc =
r" If there is only a single item, then remove the braces (`use test::A;`"]
#[doc = r" for example)."]
#[doc = r""]
#[doc = r#" This lint is "allow" by default because it is only enforcing a"#]
#[doc = r" stylistic choice."]
static UNUSED_IMPORT_BRACES: &::rustc_lint_defs::Lint =
    &::rustc_lint_defs::Lint {
            name: "UNUSED_IMPORT_BRACES",
            default_level: ::rustc_lint_defs::Allow,
            desc: "unnecessary braces around an imported item",
            is_externally_loaded: false,
            ..::rustc_lint_defs::Lint::default_fields_for_macro()
        };declare_lint! {
1130    /// The `unused_import_braces` lint catches unnecessary braces around an
1131    /// imported item.
1132    ///
1133    /// ### Example
1134    ///
1135    /// ```rust,compile_fail
1136    /// #![deny(unused_import_braces)]
1137    /// use test::{A};
1138    ///
1139    /// pub mod test {
1140    ///     pub struct A;
1141    /// }
1142    /// # fn main() {}
1143    /// ```
1144    ///
1145    /// {{produces}}
1146    ///
1147    /// ### Explanation
1148    ///
1149    /// If there is only a single item, then remove the braces (`use test::A;`
1150    /// for example).
1151    ///
1152    /// This lint is "allow" by default because it is only enforcing a
1153    /// stylistic choice.
1154    UNUSED_IMPORT_BRACES,
1155    Allow,
1156    "unnecessary braces around an imported item"
1157}
1158
1159pub struct UnusedImportBraces;
#[automatically_derived]
impl ::core::marker::Copy for UnusedImportBraces { }
#[automatically_derived]
#[doc(hidden)]
unsafe impl ::core::clone::TrivialClone for UnusedImportBraces { }
#[automatically_derived]
impl ::core::clone::Clone for UnusedImportBraces {
    #[inline]
    fn clone(&self) -> UnusedImportBraces { *self }
}
impl ::rustc_lint_defs::LintPass for UnusedImportBraces {
    fn name(&self) -> &'static str { "UnusedImportBraces" }
    fn get_lints(&self) -> ::rustc_lint_defs::LintVec {
        ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
                [UNUSED_IMPORT_BRACES]))
    }
}
impl UnusedImportBraces {
    #[allow(unused)]
    pub fn lint_vec() -> ::rustc_lint_defs::LintVec {
        ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
                [UNUSED_IMPORT_BRACES]))
    }
}declare_lint_pass!(UnusedImportBraces => [UNUSED_IMPORT_BRACES]);
1160
1161impl UnusedImportBraces {
1162    fn check_use_tree(&self, cx: &EarlyContext<'_>, use_tree: &ast::UseTree, item: &ast::Item) {
1163        if let ast::UseTreeKind::Nested { ref items, .. } = use_tree.kind {
1164            // Recursively check nested UseTrees
1165            for (tree, _) in items {
1166                self.check_use_tree(cx, tree, item);
1167            }
1168
1169            // Trigger the lint only if there is one nested item
1170            let [(tree, _)] = items.as_slice() else { return };
1171
1172            // Trigger the lint if the nested item is a non-self single item
1173            let node_name = match tree.kind {
1174                ast::UseTreeKind::Simple(rename) => {
1175                    let orig_ident = tree.prefix.segments.last().unwrap().ident;
1176                    if orig_ident.name == kw::SelfLower {
1177                        return;
1178                    }
1179                    rename.unwrap_or(orig_ident).name
1180                }
1181                ast::UseTreeKind::Glob => sym::asterisk,
1182                ast::UseTreeKind::Nested { .. } => return,
1183            };
1184
1185            cx.emit_span_lint(
1186                UNUSED_IMPORT_BRACES,
1187                item.span,
1188                UnusedImportBracesDiag { node: node_name },
1189            );
1190        }
1191    }
1192}
1193
1194impl EarlyLintPass for UnusedImportBraces {
1195    fn check_item(&mut self, cx: &EarlyContext<'_>, item: &ast::Item) {
1196        if let ast::ItemKind::Use(ref use_tree) = item.kind {
1197            self.check_use_tree(cx, use_tree, item);
1198        }
1199    }
1200}
1201
1202#[doc =
r" The `unused_allocation` lint detects unnecessary allocations that can"]
#[doc = r" be eliminated."]
#[doc = r""]
#[doc = r" ### Example"]
#[doc = r""]
#[doc = r" ```rust"]
#[doc = r" fn main() {"]
#[doc = r"     let a = Box::new([1, 2, 3]).len();"]
#[doc = r" }"]
#[doc = r" ```"]
#[doc = r""]
#[doc = r" {{produces}}"]
#[doc = r""]
#[doc = r" ### Explanation"]
#[doc = r""]
#[doc =
r" When a `box` expression is immediately coerced to a reference, then"]
#[doc =
r" the allocation is unnecessary, and a reference (using `&` or `&mut`)"]
#[doc = r" should be used instead to avoid the allocation."]
pub(super) static UNUSED_ALLOCATION: &::rustc_lint_defs::Lint =
    &::rustc_lint_defs::Lint {
            name: "UNUSED_ALLOCATION",
            default_level: ::rustc_lint_defs::Warn,
            desc: "detects unnecessary allocations that can be eliminated",
            is_externally_loaded: false,
            ..::rustc_lint_defs::Lint::default_fields_for_macro()
        };declare_lint! {
1203    /// The `unused_allocation` lint detects unnecessary allocations that can
1204    /// be eliminated.
1205    ///
1206    /// ### Example
1207    ///
1208    /// ```rust
1209    /// fn main() {
1210    ///     let a = Box::new([1, 2, 3]).len();
1211    /// }
1212    /// ```
1213    ///
1214    /// {{produces}}
1215    ///
1216    /// ### Explanation
1217    ///
1218    /// When a `box` expression is immediately coerced to a reference, then
1219    /// the allocation is unnecessary, and a reference (using `&` or `&mut`)
1220    /// should be used instead to avoid the allocation.
1221    pub(super) UNUSED_ALLOCATION,
1222    Warn,
1223    "detects unnecessary allocations that can be eliminated"
1224}
1225
1226pub struct UnusedAllocation;
#[automatically_derived]
impl ::core::marker::Copy for UnusedAllocation { }
#[automatically_derived]
#[doc(hidden)]
unsafe impl ::core::clone::TrivialClone for UnusedAllocation { }
#[automatically_derived]
impl ::core::clone::Clone for UnusedAllocation {
    #[inline]
    fn clone(&self) -> UnusedAllocation { *self }
}
impl ::rustc_lint_defs::LintPass for UnusedAllocation {
    fn name(&self) -> &'static str { "UnusedAllocation" }
    fn get_lints(&self) -> ::rustc_lint_defs::LintVec {
        ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
                [UNUSED_ALLOCATION]))
    }
}
impl UnusedAllocation {
    #[allow(unused)]
    pub fn lint_vec() -> ::rustc_lint_defs::LintVec {
        ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
                [UNUSED_ALLOCATION]))
    }
}declare_lint_pass!(UnusedAllocation => [UNUSED_ALLOCATION]);
1227
1228impl<'tcx> LateLintPass<'tcx> for UnusedAllocation {
1229    fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &hir::Expr<'_>) {
1230        match e.kind {
1231            hir::ExprKind::Call(path_expr, [_])
1232                if let hir::ExprKind::Path(qpath) = &path_expr.kind
1233                    && let Some(did) = cx.qpath_res(qpath, path_expr.hir_id).opt_def_id()
1234                    && cx.tcx.is_diagnostic_item(sym::box_new, did) => {}
1235            _ => return,
1236        }
1237
1238        for adj in cx.typeck_results().expr_adjustments(e) {
1239            if let adjustment::Adjust::Borrow(adjustment::AutoBorrow::Ref(m)) = adj.kind {
1240                if let ty::Ref(_, inner_ty, _) = adj.target.kind()
1241                    && inner_ty.is_box()
1242                {
1243                    // If the target type is `&Box<T>` or `&mut Box<T>`, the allocation is necessary
1244                    continue;
1245                }
1246                match m {
1247                    adjustment::AutoBorrowMutability::Not => {
1248                        cx.emit_span_lint(UNUSED_ALLOCATION, e.span, UnusedAllocationDiag);
1249                    }
1250                    adjustment::AutoBorrowMutability::Mut { .. } => {
1251                        cx.emit_span_lint(UNUSED_ALLOCATION, e.span, UnusedAllocationMutDiag);
1252                    }
1253                };
1254            }
1255        }
1256    }
1257}