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, PatKind};
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::MUT, ..) if avoid_mut => {
654 return;
655 }
656 _ => {}
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 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 Paren(_) => {}
765 Missing
767 | Wild
768 | Never
769 | Rest
770 | Expr(..)
771 | MacCall(..)
772 | Range(..)
773 | Ident(.., None)
774 | Path(..)
775 | Err(_) => {}
776 TupleStruct(_, _, ps) | Tuple(ps) | Slice(ps) | Or(ps) => {
778 for p in ps {
779 self.check_unused_parens_pat(cx, p, false, false, keep_space);
780 }
781 }
782 Struct(_, _, fps, _) => {
783 for f in fps {
784 self.check_unused_parens_pat(cx, &f.pat, false, false, keep_space);
785 }
786 }
787 Ident(.., Some(p)) | Box(p) | Deref(p) | Guard(p, _) => {
789 self.check_unused_parens_pat(cx, p, true, false, keep_space)
790 }
791 Ref(p, _, m) => {
795 self.check_unused_parens_pat(cx, p, true, *m == Mutability::Not, keep_space)
796 }
797 }
798 }
799
800 fn check_stmt(&mut self, cx: &EarlyContext<'_>, s: &ast::Stmt) {
801 if let StmtKind::Let(ref local) = s.kind {
802 self.check_unused_parens_pat(cx, &local.pat, true, false, (true, false));
803 }
804
805 <Self as UnusedDelimLint>::check_stmt(self, cx, s)
806 }
807
808 fn check_param(&mut self, cx: &EarlyContext<'_>, param: &ast::Param) {
809 self.check_unused_parens_pat(cx, ¶m.pat, true, false, (false, false));
810 }
811
812 fn check_arm(&mut self, cx: &EarlyContext<'_>, arm: &ast::Arm) {
813 self.check_unused_parens_pat(cx, &arm.pat, false, false, (false, false));
814 }
815
816 fn check_ty(&mut self, cx: &EarlyContext<'_>, ty: &ast::Ty) {
817 if let ast::TyKind::Paren(_) = ty.kind
818 && Some(&ty.id) == self.parens_in_cast_in_lt.last()
819 {
820 return;
821 }
822 match &ty.kind {
823 ast::TyKind::Array(_, len) => {
824 self.check_unused_delims_expr(
825 cx,
826 &len.value,
827 UnusedDelimsCtx::ArrayLenExpr,
828 false,
829 None,
830 None,
831 false,
832 );
833 }
834 ast::TyKind::Paren(r) => {
835 let unused_parens = match &r.kind {
836 ast::TyKind::ImplTrait(_, bounds) | ast::TyKind::TraitObject(bounds, _) => {
837 match self.in_no_bounds_pos.get(&ty.id) {
838 Some(NoBoundsException::None) => false,
839 Some(NoBoundsException::OneBound) => bounds.len() <= 1,
840 None => true,
841 }
842 }
843 ast::TyKind::FnPtr(b) => {
844 !self.with_self_ty_parens || b.generic_params.is_empty()
845 }
846 _ => true,
847 };
848
849 if unused_parens {
850 let spans = (!ty.span.from_expansion())
851 .then(|| {
852 r.span
853 .find_ancestor_inside(ty.span)
854 .map(|r| (ty.span.with_hi(r.lo()), ty.span.with_lo(r.hi())))
855 })
856 .flatten();
857
858 self.emit_unused_delims(cx, ty.span, spans, "type", (false, false), false);
859 }
860
861 self.with_self_ty_parens = false;
862 }
863 ast::TyKind::Ref(_, mut_ty) | ast::TyKind::Ptr(mut_ty) => {
864 let own_constraint = self.in_no_bounds_pos.get(&ty.id);
867 let constraint = match own_constraint {
868 Some(NoBoundsException::None) => NoBoundsException::None,
869 Some(NoBoundsException::OneBound) => NoBoundsException::OneBound,
870 None => NoBoundsException::OneBound,
871 };
872 self.in_no_bounds_pos.insert(mut_ty.ty.id, constraint);
873 }
874 ast::TyKind::TraitObject(bounds, _) | ast::TyKind::ImplTrait(_, bounds) => {
875 for i in 0..bounds.len() {
876 let is_last = i == bounds.len() - 1;
877
878 if let ast::GenericBound::Trait(poly_trait_ref) = &bounds[i] {
879 let fn_with_explicit_ret_ty = if let [.., segment] =
880 &*poly_trait_ref.trait_ref.path.segments
881 && let Some(args) = segment.args.as_ref()
882 && let ast::GenericArgs::Parenthesized(paren_args) = &**args
883 && let ast::FnRetTy::Ty(ret_ty) = &paren_args.output
884 {
885 self.in_no_bounds_pos.insert(
886 ret_ty.id,
887 if is_last {
888 NoBoundsException::OneBound
889 } else {
890 NoBoundsException::None
891 },
892 );
893
894 true
895 } else {
896 false
897 };
898
899 let dyn2015_exception = cx.sess().psess.edition == Edition2015
904 && #[allow(non_exhaustive_omitted_patterns)] match ty.kind {
ast::TyKind::TraitObject(..) => true,
_ => false,
}matches!(ty.kind, ast::TyKind::TraitObject(..))
905 && i == 0
906 && poly_trait_ref
907 .trait_ref
908 .path
909 .segments
910 .first()
911 .map(|s| s.ident.name == kw::PathRoot)
912 .unwrap_or(false);
913
914 if let ast::Parens::Yes = poly_trait_ref.parens
915 && (is_last || !fn_with_explicit_ret_ty)
916 && !dyn2015_exception
917 {
918 let s = poly_trait_ref.span;
919 let spans = (!s.from_expansion()).then(|| {
920 (
921 s.with_hi(s.lo() + rustc_span::BytePos(1)),
922 s.with_lo(s.hi() - rustc_span::BytePos(1)),
923 )
924 });
925
926 self.emit_unused_delims(
927 cx,
928 poly_trait_ref.span,
929 spans,
930 "type",
931 (false, false),
932 false,
933 );
934 }
935 }
936 }
937 }
938 _ => {}
939 }
940 }
941
942 fn check_item(&mut self, cx: &EarlyContext<'_>, item: &ast::Item) {
943 <Self as UnusedDelimLint>::check_item(self, cx, item)
944 }
945
946 fn check_item_post(&mut self, _: &EarlyContext<'_>, _: &rustc_ast::Item) {
947 self.in_no_bounds_pos.clear();
948 }
949
950 fn enter_where_predicate(&mut self, _: &EarlyContext<'_>, pred: &ast::WherePredicate) {
951 use rustc_ast::{WhereBoundPredicate, WherePredicateKind};
952 if let WherePredicateKind::BoundPredicate(WhereBoundPredicate {
953 bounded_ty,
954 bound_generic_params,
955 ..
956 }) = &pred.kind
957 && let ast::TyKind::Paren(_) = &bounded_ty.kind
958 && bound_generic_params.is_empty()
959 {
960 self.with_self_ty_parens = true;
961 }
962 }
963
964 fn exit_where_predicate(&mut self, _: &EarlyContext<'_>, _: &ast::WherePredicate) {
965 if !!self.with_self_ty_parens {
::core::panicking::panic("assertion failed: !self.with_self_ty_parens")
};assert!(!self.with_self_ty_parens);
966 }
967}
968
969#[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! {
970 pub(super) UNUSED_BRACES,
988 Warn,
989 "unnecessary braces around an expression"
990}
991
992pub 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]);
993
994impl UnusedDelimLint for UnusedBraces {
995 const DELIM_STR: &'static str = "braces";
996
997 const LINT_EXPR_IN_PATTERN_MATCHING_CTX: bool = false;
998
999 fn lint(&self) -> &'static Lint {
1000 UNUSED_BRACES
1001 }
1002
1003 fn check_unused_delims_expr(
1004 &self,
1005 cx: &EarlyContext<'_>,
1006 value: &ast::Expr,
1007 ctx: UnusedDelimsCtx,
1008 followed_by_block: bool,
1009 left_pos: Option<BytePos>,
1010 right_pos: Option<BytePos>,
1011 is_kw: bool,
1012 ) {
1013 match value.kind {
1014 ast::ExprKind::Block(ref inner, None)
1015 if inner.rules == ast::BlockCheckMode::Default =>
1016 {
1017 if let [stmt] = inner.stmts.as_slice()
1042 && let ast::StmtKind::Expr(ref expr) = stmt.kind
1043 && !Self::is_expr_delims_necessary(expr, ctx, followed_by_block)
1044 && (ctx != UnusedDelimsCtx::AnonConst
1045 || (#[allow(non_exhaustive_omitted_patterns)] match expr.kind {
ast::ExprKind::Lit(_) => true,
_ => false,
}matches!(expr.kind, ast::ExprKind::Lit(_))
1046 && !expr.span.from_expansion()))
1047 && ctx != UnusedDelimsCtx::ClosureBody
1048 && !cx.sess().source_map().is_multiline(value.span)
1049 && value.attrs.is_empty()
1050 && !value.span.from_expansion()
1051 && !inner.span.from_expansion()
1052 {
1053 self.emit_unused_delims_expr(cx, value, ctx, left_pos, right_pos, is_kw)
1054 }
1055 }
1056 ast::ExprKind::Let(_, ref expr, _, _) => {
1057 self.check_unused_delims_expr(
1058 cx,
1059 expr,
1060 UnusedDelimsCtx::LetScrutineeExpr,
1061 followed_by_block,
1062 None,
1063 None,
1064 false,
1065 );
1066 }
1067 _ => {}
1068 }
1069 }
1070}
1071
1072impl EarlyLintPass for UnusedBraces {
1073 fn check_stmt(&mut self, cx: &EarlyContext<'_>, s: &ast::Stmt) {
1074 <Self as UnusedDelimLint>::check_stmt(self, cx, s)
1075 }
1076
1077 #[inline]
1078 fn check_expr(&mut self, cx: &EarlyContext<'_>, e: &ast::Expr) {
1079 <Self as UnusedDelimLint>::check_expr(self, cx, e);
1080
1081 if let ExprKind::Repeat(_, ref anon_const) = e.kind {
1082 self.check_unused_delims_expr(
1083 cx,
1084 &anon_const.value,
1085 UnusedDelimsCtx::AnonConst,
1086 false,
1087 None,
1088 None,
1089 false,
1090 );
1091 }
1092 }
1093
1094 fn check_generic_arg(&mut self, cx: &EarlyContext<'_>, arg: &ast::GenericArg) {
1095 if let ast::GenericArg::Const(ct) = arg {
1096 self.check_unused_delims_expr(
1097 cx,
1098 &ct.value,
1099 UnusedDelimsCtx::AnonConst,
1100 false,
1101 None,
1102 None,
1103 false,
1104 );
1105 }
1106 }
1107
1108 fn check_variant(&mut self, cx: &EarlyContext<'_>, v: &ast::Variant) {
1109 if let Some(anon_const) = &v.disr_expr {
1110 self.check_unused_delims_expr(
1111 cx,
1112 &anon_const.value,
1113 UnusedDelimsCtx::AnonConst,
1114 false,
1115 None,
1116 None,
1117 false,
1118 );
1119 }
1120 }
1121
1122 fn check_ty(&mut self, cx: &EarlyContext<'_>, ty: &ast::Ty) {
1123 match ty.kind {
1124 ast::TyKind::Array(_, ref len) => {
1125 self.check_unused_delims_expr(
1126 cx,
1127 &len.value,
1128 UnusedDelimsCtx::ArrayLenExpr,
1129 false,
1130 None,
1131 None,
1132 false,
1133 );
1134 }
1135
1136 _ => {}
1137 }
1138 }
1139
1140 fn check_item(&mut self, cx: &EarlyContext<'_>, item: &ast::Item) {
1141 <Self as UnusedDelimLint>::check_item(self, cx, item)
1142 }
1143}
1144
1145#[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! {
1146 UNUSED_IMPORT_BRACES,
1171 Allow,
1172 "unnecessary braces around an imported item"
1173}
1174
1175pub 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]);
1176
1177impl UnusedImportBraces {
1178 fn check_use_tree(&self, cx: &EarlyContext<'_>, use_tree: &ast::UseTree, item: &ast::Item) {
1179 if let ast::UseTreeKind::Nested { ref items, .. } = use_tree.kind {
1180 for (tree, _) in items {
1182 self.check_use_tree(cx, tree, item);
1183 }
1184
1185 let [(tree, _)] = items.as_slice() else { return };
1187
1188 let node_name = match tree.kind {
1190 ast::UseTreeKind::Simple(rename) => {
1191 let orig_ident = tree.prefix.segments.last().unwrap().ident;
1192 if orig_ident.name == kw::SelfLower {
1193 return;
1194 }
1195 rename.unwrap_or(orig_ident).name
1196 }
1197 ast::UseTreeKind::Glob(_) => sym::asterisk,
1198 ast::UseTreeKind::Nested { .. } => return,
1199 };
1200
1201 cx.emit_span_lint(
1202 UNUSED_IMPORT_BRACES,
1203 item.span,
1204 UnusedImportBracesDiag { node: node_name },
1205 );
1206 }
1207 }
1208}
1209
1210impl EarlyLintPass for UnusedImportBraces {
1211 fn check_item(&mut self, cx: &EarlyContext<'_>, item: &ast::Item) {
1212 if let ast::ItemKind::Use(ref use_tree) = item.kind {
1213 self.check_use_tree(cx, use_tree, item);
1214 }
1215 }
1216}
1217
1218#[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! {
1219 pub(super) UNUSED_ALLOCATION,
1238 Warn,
1239 "detects unnecessary allocations that can be eliminated"
1240}
1241
1242pub 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]);
1243
1244impl<'tcx> LateLintPass<'tcx> for UnusedAllocation {
1245 fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &hir::Expr<'_>) {
1246 match e.kind {
1247 hir::ExprKind::Call(path_expr, [_])
1248 if let hir::ExprKind::Path(qpath) = &path_expr.kind
1249 && let Some(did) = cx.qpath_res(qpath, path_expr.hir_id).opt_def_id()
1250 && cx.tcx.is_diagnostic_item(sym::box_new, did) => {}
1251 _ => return,
1252 }
1253
1254 for adj in cx.typeck_results().expr_adjustments(e) {
1255 if let adjustment::Adjust::Borrow(adjustment::AutoBorrow::Ref(m)) = adj.kind {
1256 if let ty::Ref(_, inner_ty, _) = adj.target.kind()
1257 && inner_ty.is_box()
1258 {
1259 continue;
1261 }
1262 match m {
1263 adjustment::AutoBorrowMutability::Not => {
1264 cx.emit_span_lint(UNUSED_ALLOCATION, e.span, UnusedAllocationDiag);
1265 }
1266 adjustment::AutoBorrowMutability::Mut { .. } => {
1267 cx.emit_span_lint(UNUSED_ALLOCATION, e.span, UnusedAllocationMutDiag);
1268 }
1269 };
1270 }
1271 }
1272 }
1273}