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 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
107trait UnusedDelimLint {
109 const DELIM_STR: &'static str;
110
111 const LINT_EXPR_IN_PATTERN_MATCHING_CTX: bool;
123
124 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 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 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 {
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 if !followed_by_block {
216 return false;
217 }
218
219 {
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 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 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 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 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 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 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 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 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 _ => {
430 return;
431 }
432 };
433 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 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(ast::ConstItem { rhs_kind, .. }) = &item.kind {
503 if let Some(e) = rhs_kind.expr() { e } else { return }
504 } else if let Static(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 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 parens_in_cast_in_lt: Vec<ast::NodeId>,
548 in_no_bounds_pos: FxHashMap<ast::NodeId, NoBoundsException>,
551}
552
553enum NoBoundsException {
568 None,
570 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, ByRef, Mutability, PatKind, Pinnedness};
640
641 if let PatKind::Paren(inner) = &value.kind {
642 match inner.kind {
643 PatKind::Range(..) => return,
648 PatKind::Guard(..) => return,
650 PatKind::Or(..) if avoid_or => return,
652 PatKind::Ident(BindingMode(_, Mutability::Mut), ..) if avoid_mut => {
655 return;
656 }
657 PatKind::Ref(_, Pinnedness::Pinned, _)
658 | PatKind::Ident(BindingMode(ByRef::Yes(Pinnedness::Pinned, _), _), ..)
659 if !cx.builder.features().pin_ergonomics() =>
661 {
662 return;
663 }
664 _ => {}
666 }
667 let spans = if !value.span.from_expansion() {
668 inner
669 .span
670 .find_ancestor_inside(value.span)
671 .map(|inner| (value.span.with_hi(inner.lo()), value.span.with_lo(inner.hi())))
672 } else {
673 None
674 };
675 self.emit_unused_delims(cx, value.span, spans, "pattern", keep_space, false);
676 }
677 }
678
679 fn cast_followed_by_lt(&self, expr: &ast::Expr) -> Option<ast::NodeId> {
680 if let ExprKind::Binary(op, lhs, _rhs) = &expr.kind
681 && (op.node == ast::BinOpKind::Lt || op.node == ast::BinOpKind::Shl)
682 {
683 let mut cur = lhs;
684 while let ExprKind::Binary(_, _, rhs) = &cur.kind {
685 cur = rhs;
686 }
687
688 if let ExprKind::Cast(_, ty) = &cur.kind
689 && let ast::TyKind::Paren(_) = &ty.kind
690 {
691 return Some(ty.id);
692 }
693 }
694 None
695 }
696}
697
698impl EarlyLintPass for UnusedParens {
699 #[inline]
700 fn check_expr(&mut self, cx: &EarlyContext<'_>, e: &ast::Expr) {
701 if let Some(ty_id) = self.cast_followed_by_lt(e) {
702 self.parens_in_cast_in_lt.push(ty_id);
703 }
704
705 match e.kind {
706 ExprKind::Let(ref pat, _, _, _) | ExprKind::ForLoop { ref pat, .. } => {
707 self.check_unused_parens_pat(cx, pat, false, false, (true, true));
708 }
709 ExprKind::If(ref cond, ref block, ref else_)
713 if #[allow(non_exhaustive_omitted_patterns)] match cond.peel_parens().kind {
ExprKind::Let(..) => true,
_ => false,
}matches!(cond.peel_parens().kind, ExprKind::Let(..)) =>
714 {
715 self.check_unused_delims_expr(
716 cx,
717 cond.peel_parens(),
718 UnusedDelimsCtx::LetScrutineeExpr,
719 true,
720 None,
721 None,
722 true,
723 );
724 for stmt in &block.stmts {
725 <Self as UnusedDelimLint>::check_stmt(self, cx, stmt);
726 }
727 if let Some(e) = else_ {
728 <Self as UnusedDelimLint>::check_expr(self, cx, e);
729 }
730 return;
731 }
732 ExprKind::Match(ref _expr, ref arm, _) => {
733 for a in arm {
734 if let Some(body) = &a.body {
735 self.check_unused_delims_expr(
736 cx,
737 body,
738 UnusedDelimsCtx::MatchArmExpr,
739 false,
740 None,
741 None,
742 true,
743 );
744 }
745 }
746 }
747 _ => {}
748 }
749
750 <Self as UnusedDelimLint>::check_expr(self, cx, e)
751 }
752
753 fn check_expr_post(&mut self, _cx: &EarlyContext<'_>, e: &ast::Expr) {
754 if let Some(ty_id) = self.cast_followed_by_lt(e) {
755 let id = self
756 .parens_in_cast_in_lt
757 .pop()
758 .expect("check_expr and check_expr_post must balance");
759 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!(
760 id, ty_id,
761 "check_expr, check_ty, and check_expr_post are called, in that order, by the visitor"
762 );
763 }
764 }
765
766 fn check_pat(&mut self, cx: &EarlyContext<'_>, p: &ast::Pat) {
767 use ast::PatKind::*;
768 use ast::{Mutability, Pinnedness};
769 let keep_space = (false, false);
770 match &p.kind {
771 Paren(_) => {}
773 Missing
775 | Wild
776 | Never
777 | Rest
778 | Expr(..)
779 | MacCall(..)
780 | Range(..)
781 | Ident(.., None)
782 | Path(..)
783 | Err(_) => {}
784 TupleStruct(_, _, ps) | Tuple(ps) | Slice(ps) | Or(ps) => {
786 for p in ps {
787 self.check_unused_parens_pat(cx, p, false, false, keep_space);
788 }
789 }
790 Struct(_, _, fps, _) => {
791 for f in fps {
792 self.check_unused_parens_pat(cx, &f.pat, false, false, keep_space);
793 }
794 }
795 Ident(.., Some(p)) | Box(p) | Deref(p) | Guard(p, _) => {
797 self.check_unused_parens_pat(cx, p, true, false, keep_space)
798 }
799 Ref(p, pinned, m)
805 if *pinned != Pinnedness::Pinned
806 || cx.builder.features().pin_ergonomics() =>
808 {
809 self.check_unused_parens_pat(
810 cx,
811 p,
812 true,
813 *pinned == Pinnedness::Not && *m == Mutability::Not,
814 keep_space,
815 );
816 }
817 Ref(..) => {}
818 }
819 }
820
821 fn check_stmt(&mut self, cx: &EarlyContext<'_>, s: &ast::Stmt) {
822 if let StmtKind::Let(ref local) = s.kind {
823 self.check_unused_parens_pat(cx, &local.pat, true, false, (true, false));
824 }
825
826 <Self as UnusedDelimLint>::check_stmt(self, cx, s)
827 }
828
829 fn check_param(&mut self, cx: &EarlyContext<'_>, param: &ast::Param) {
830 self.check_unused_parens_pat(cx, ¶m.pat, true, false, (false, false));
831 }
832
833 fn check_arm(&mut self, cx: &EarlyContext<'_>, arm: &ast::Arm) {
834 self.check_unused_parens_pat(cx, &arm.pat, false, false, (false, false));
835 }
836
837 fn check_ty(&mut self, cx: &EarlyContext<'_>, ty: &ast::Ty) {
838 if let ast::TyKind::Paren(_) = ty.kind
839 && Some(&ty.id) == self.parens_in_cast_in_lt.last()
840 {
841 return;
842 }
843 match &ty.kind {
844 ast::TyKind::Array(_, len) => {
845 self.check_unused_delims_expr(
846 cx,
847 &len.value,
848 UnusedDelimsCtx::ArrayLenExpr,
849 false,
850 None,
851 None,
852 false,
853 );
854 }
855 ast::TyKind::Paren(r) => {
856 let unused_parens = match &r.kind {
857 ast::TyKind::ImplTrait(_, bounds) | ast::TyKind::TraitObject(bounds, _) => {
858 match self.in_no_bounds_pos.get(&ty.id) {
859 Some(NoBoundsException::None) => false,
860 Some(NoBoundsException::OneBound) => bounds.len() <= 1,
861 None => true,
862 }
863 }
864 ast::TyKind::FnPtr(b) => {
865 !self.with_self_ty_parens || b.generic_params.is_empty()
866 }
867 _ => true,
868 };
869
870 if unused_parens {
871 let spans = (!ty.span.from_expansion())
872 .then(|| {
873 r.span
874 .find_ancestor_inside(ty.span)
875 .map(|r| (ty.span.with_hi(r.lo()), ty.span.with_lo(r.hi())))
876 })
877 .flatten();
878
879 self.emit_unused_delims(cx, ty.span, spans, "type", (false, false), false);
880 }
881
882 self.with_self_ty_parens = false;
883 }
884 ast::TyKind::Ref(_, mut_ty) | ast::TyKind::Ptr(mut_ty) => {
885 let own_constraint = self.in_no_bounds_pos.get(&ty.id);
888 let constraint = match own_constraint {
889 Some(NoBoundsException::None) => NoBoundsException::None,
890 Some(NoBoundsException::OneBound) => NoBoundsException::OneBound,
891 None => NoBoundsException::OneBound,
892 };
893 self.in_no_bounds_pos.insert(mut_ty.ty.id, constraint);
894 }
895 ast::TyKind::TraitObject(bounds, _) | ast::TyKind::ImplTrait(_, bounds) => {
896 for i in 0..bounds.len() {
897 let is_last = i == bounds.len() - 1;
898
899 if let ast::GenericBound::Trait(poly_trait_ref) = &bounds[i] {
900 let fn_with_explicit_ret_ty = if let [.., segment] =
901 &*poly_trait_ref.trait_ref.path.segments
902 && let Some(args) = segment.args.as_ref()
903 && let ast::GenericArgs::Parenthesized(paren_args) = &**args
904 && let ast::FnRetTy::Ty(ret_ty) = &paren_args.output
905 {
906 self.in_no_bounds_pos.insert(
907 ret_ty.id,
908 if is_last {
909 NoBoundsException::OneBound
910 } else {
911 NoBoundsException::None
912 },
913 );
914
915 true
916 } else {
917 false
918 };
919
920 let dyn2015_exception = cx.sess().psess.edition == Edition2015
925 && #[allow(non_exhaustive_omitted_patterns)] match ty.kind {
ast::TyKind::TraitObject(..) => true,
_ => false,
}matches!(ty.kind, ast::TyKind::TraitObject(..))
926 && i == 0
927 && poly_trait_ref
928 .trait_ref
929 .path
930 .segments
931 .first()
932 .map(|s| s.ident.name == kw::PathRoot)
933 .unwrap_or(false);
934
935 if let ast::Parens::Yes = poly_trait_ref.parens
936 && (is_last || !fn_with_explicit_ret_ty)
937 && !dyn2015_exception
938 {
939 let s = poly_trait_ref.span;
940 let spans = (!s.from_expansion()).then(|| {
941 (
942 s.with_hi(s.lo() + rustc_span::BytePos(1)),
943 s.with_lo(s.hi() - rustc_span::BytePos(1)),
944 )
945 });
946
947 self.emit_unused_delims(
948 cx,
949 poly_trait_ref.span,
950 spans,
951 "type",
952 (false, false),
953 false,
954 );
955 }
956 }
957 }
958 }
959 _ => {}
960 }
961 }
962
963 fn check_item(&mut self, cx: &EarlyContext<'_>, item: &ast::Item) {
964 <Self as UnusedDelimLint>::check_item(self, cx, item)
965 }
966
967 fn check_item_post(&mut self, _: &EarlyContext<'_>, _: &rustc_ast::Item) {
968 self.in_no_bounds_pos.clear();
969 }
970
971 fn enter_where_predicate(&mut self, _: &EarlyContext<'_>, pred: &ast::WherePredicate) {
972 use rustc_ast::{WhereBoundPredicate, WherePredicateKind};
973 if let WherePredicateKind::BoundPredicate(WhereBoundPredicate {
974 bounded_ty,
975 bound_generic_params,
976 ..
977 }) = &pred.kind
978 && let ast::TyKind::Paren(_) = &bounded_ty.kind
979 && bound_generic_params.is_empty()
980 {
981 self.with_self_ty_parens = true;
982 }
983 }
984
985 fn exit_where_predicate(&mut self, _: &EarlyContext<'_>, _: &ast::WherePredicate) {
986 if !!self.with_self_ty_parens {
::core::panicking::panic("assertion failed: !self.with_self_ty_parens")
};assert!(!self.with_self_ty_parens);
987 }
988}
989
990#[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! {
991 pub(super) UNUSED_BRACES,
1009 Warn,
1010 "unnecessary braces around an expression"
1011}
1012
1013pub 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]);
1014
1015impl UnusedDelimLint for UnusedBraces {
1016 const DELIM_STR: &'static str = "braces";
1017
1018 const LINT_EXPR_IN_PATTERN_MATCHING_CTX: bool = false;
1019
1020 fn lint(&self) -> &'static Lint {
1021 UNUSED_BRACES
1022 }
1023
1024 fn check_unused_delims_expr(
1025 &self,
1026 cx: &EarlyContext<'_>,
1027 value: &ast::Expr,
1028 ctx: UnusedDelimsCtx,
1029 followed_by_block: bool,
1030 left_pos: Option<BytePos>,
1031 right_pos: Option<BytePos>,
1032 is_kw: bool,
1033 ) {
1034 match value.kind {
1035 ast::ExprKind::Block(ref inner, None)
1036 if inner.rules == ast::BlockCheckMode::Default =>
1037 {
1038 if let [stmt] = inner.stmts.as_slice()
1063 && let ast::StmtKind::Expr(ref expr) = stmt.kind
1064 && !Self::is_expr_delims_necessary(expr, ctx, followed_by_block)
1065 && (ctx != UnusedDelimsCtx::AnonConst
1066 || (#[allow(non_exhaustive_omitted_patterns)] match expr.kind {
ast::ExprKind::Lit(_) => true,
_ => false,
}matches!(expr.kind, ast::ExprKind::Lit(_))
1067 && !expr.span.from_expansion()))
1068 && ctx != UnusedDelimsCtx::ClosureBody
1069 && !cx.sess().source_map().is_multiline(value.span)
1070 && value.attrs.is_empty()
1071 && !value.span.from_expansion()
1072 && !inner.span.from_expansion()
1073 {
1074 self.emit_unused_delims_expr(cx, value, ctx, left_pos, right_pos, is_kw)
1075 }
1076 }
1077 ast::ExprKind::Let(_, ref expr, _, _) => {
1078 self.check_unused_delims_expr(
1079 cx,
1080 expr,
1081 UnusedDelimsCtx::LetScrutineeExpr,
1082 followed_by_block,
1083 None,
1084 None,
1085 false,
1086 );
1087 }
1088 _ => {}
1089 }
1090 }
1091}
1092
1093impl EarlyLintPass for UnusedBraces {
1094 fn check_stmt(&mut self, cx: &EarlyContext<'_>, s: &ast::Stmt) {
1095 <Self as UnusedDelimLint>::check_stmt(self, cx, s)
1096 }
1097
1098 #[inline]
1099 fn check_expr(&mut self, cx: &EarlyContext<'_>, e: &ast::Expr) {
1100 <Self as UnusedDelimLint>::check_expr(self, cx, e);
1101
1102 if let ExprKind::Repeat(_, ref anon_const) = e.kind {
1103 self.check_unused_delims_expr(
1104 cx,
1105 &anon_const.value,
1106 UnusedDelimsCtx::AnonConst,
1107 false,
1108 None,
1109 None,
1110 false,
1111 );
1112 }
1113 }
1114
1115 fn check_generic_arg(&mut self, cx: &EarlyContext<'_>, arg: &ast::GenericArg) {
1116 if let ast::GenericArg::Const(ct) = arg {
1117 self.check_unused_delims_expr(
1118 cx,
1119 &ct.value,
1120 UnusedDelimsCtx::AnonConst,
1121 false,
1122 None,
1123 None,
1124 false,
1125 );
1126 }
1127 }
1128
1129 fn check_variant(&mut self, cx: &EarlyContext<'_>, v: &ast::Variant) {
1130 if let Some(anon_const) = &v.disr_expr {
1131 self.check_unused_delims_expr(
1132 cx,
1133 &anon_const.value,
1134 UnusedDelimsCtx::AnonConst,
1135 false,
1136 None,
1137 None,
1138 false,
1139 );
1140 }
1141 }
1142
1143 fn check_ty(&mut self, cx: &EarlyContext<'_>, ty: &ast::Ty) {
1144 match ty.kind {
1145 ast::TyKind::Array(_, ref len) => {
1146 self.check_unused_delims_expr(
1147 cx,
1148 &len.value,
1149 UnusedDelimsCtx::ArrayLenExpr,
1150 false,
1151 None,
1152 None,
1153 false,
1154 );
1155 }
1156
1157 _ => {}
1158 }
1159 }
1160
1161 fn check_item(&mut self, cx: &EarlyContext<'_>, item: &ast::Item) {
1162 <Self as UnusedDelimLint>::check_item(self, cx, item)
1163 }
1164}
1165
1166#[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! {
1167 UNUSED_IMPORT_BRACES,
1192 Allow,
1193 "unnecessary braces around an imported item"
1194}
1195
1196pub 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]);
1197
1198impl UnusedImportBraces {
1199 fn check_use_tree(&self, cx: &EarlyContext<'_>, use_tree: &ast::UseTree, item: &ast::Item) {
1200 if let ast::UseTreeKind::Nested { ref items, .. } = use_tree.kind {
1201 for (tree, _) in items {
1203 self.check_use_tree(cx, tree, item);
1204 }
1205
1206 let [(tree, _)] = items.as_slice() else { return };
1208
1209 let node_name = match tree.kind {
1211 ast::UseTreeKind::Simple(rename) => {
1212 let orig_ident = tree.prefix.segments.last().unwrap().ident;
1213 if orig_ident.name == kw::SelfLower {
1214 return;
1215 }
1216 rename.unwrap_or(orig_ident).name
1217 }
1218 ast::UseTreeKind::Glob(_) => sym::asterisk,
1219 ast::UseTreeKind::Nested { .. } => return,
1220 };
1221
1222 cx.emit_span_lint(
1223 UNUSED_IMPORT_BRACES,
1224 item.span,
1225 UnusedImportBracesDiag { node: node_name },
1226 );
1227 }
1228 }
1229}
1230
1231impl EarlyLintPass for UnusedImportBraces {
1232 fn check_item(&mut self, cx: &EarlyContext<'_>, item: &ast::Item) {
1233 if let ast::ItemKind::Use(ref use_tree) = item.kind {
1234 self.check_use_tree(cx, use_tree, item);
1235 }
1236 }
1237}
1238
1239#[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! {
1240 pub(super) UNUSED_ALLOCATION,
1259 Warn,
1260 "detects unnecessary allocations that can be eliminated"
1261}
1262
1263pub 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]);
1264
1265impl<'tcx> LateLintPass<'tcx> for UnusedAllocation {
1266 fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &hir::Expr<'_>) {
1267 match e.kind {
1268 hir::ExprKind::Call(path_expr, [_])
1269 if let hir::ExprKind::Path(qpath) = &path_expr.kind
1270 && let Some(did) = cx.qpath_res(qpath, path_expr.hir_id).opt_def_id()
1271 && cx.tcx.is_diagnostic_item(sym::box_new, did) => {}
1272 _ => return,
1273 }
1274
1275 for adj in cx.typeck_results().expr_adjustments(e) {
1276 if let adjustment::Adjust::Borrow(adjustment::AutoBorrow::Ref(m)) = adj.kind {
1277 if let ty::Ref(_, inner_ty, _) = adj.target.kind()
1278 && inner_ty.is_box()
1279 {
1280 continue;
1282 }
1283 match m {
1284 adjustment::AutoBorrowMutability::Not => {
1285 cx.emit_span_lint(UNUSED_ALLOCATION, e.span, UnusedAllocationDiag);
1286 }
1287 adjustment::AutoBorrowMutability::Mut { .. } => {
1288 cx.emit_span_lint(UNUSED_ALLOCATION, e.span, UnusedAllocationMutDiag);
1289 }
1290 };
1291 }
1292 }
1293 }
1294}