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 {
#[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 }
354 });
355 cx.emit_span_lint(
356 self.lint(),
357 primary_span,
358 UnusedDelim { delim: Self::DELIM_STR, item: msg, suggestion },
359 );
360 }
361
362 fn check_expr(&mut self, cx: &EarlyContext<'_>, e: &ast::Expr) {
363 use rustc_ast::ExprKind::*;
364 let (value, ctx, followed_by_block, left_pos, right_pos, is_kw) = match e.kind {
365 If(ref cond, ref block, _)
367 if !#[allow(non_exhaustive_omitted_patterns)] match cond.kind {
Let(..) => true,
_ => false,
}matches!(cond.kind, Let(..)) || Self::LINT_EXPR_IN_PATTERN_MATCHING_CTX =>
368 {
369 let left = e.span.lo() + rustc_span::BytePos(2);
370 let right = block.span.lo();
371 (cond, UnusedDelimsCtx::IfCond, true, Some(left), Some(right), true)
372 }
373
374 While(ref cond, ref block, ..)
376 if !#[allow(non_exhaustive_omitted_patterns)] match cond.kind {
Let(..) => true,
_ => false,
}matches!(cond.kind, Let(..)) || Self::LINT_EXPR_IN_PATTERN_MATCHING_CTX =>
377 {
378 let left = e.span.lo() + rustc_span::BytePos(5);
379 let right = block.span.lo();
380 (cond, UnusedDelimsCtx::WhileCond, true, Some(left), Some(right), true)
381 }
382
383 ForLoop { ref iter, ref body, .. } => {
384 (iter, UnusedDelimsCtx::ForIterExpr, true, None, Some(body.span.lo()), true)
385 }
386
387 Match(ref head, _, ast::MatchKind::Prefix)
388 if Self::LINT_EXPR_IN_PATTERN_MATCHING_CTX =>
389 {
390 let left = e.span.lo() + rustc_span::BytePos(5);
391 (head, UnusedDelimsCtx::MatchScrutineeExpr, true, Some(left), None, true)
392 }
393
394 Ret(Some(ref value)) => {
395 let left = e.span.lo() + rustc_span::BytePos(3);
396 (value, UnusedDelimsCtx::ReturnValue, false, Some(left), None, true)
397 }
398
399 Break(label, Some(ref value)) => {
400 if label.is_some()
404 && #[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)
405 if matches!(inner.kind, ast::ExprKind::Block(..)))
406 {
407 return;
408 }
409 (value, UnusedDelimsCtx::BreakValue, false, None, None, true)
410 }
411
412 Index(_, ref value, _) => (value, UnusedDelimsCtx::IndexExpr, false, None, None, false),
413
414 Assign(_, ref value, _) | AssignOp(.., ref value) => {
415 (value, UnusedDelimsCtx::AssignedValue, false, None, None, false)
416 }
417 ref call_or_other => {
419 let (args_to_check, ctx) = match *call_or_other {
420 Call(_, ref args) => (&args[..], UnusedDelimsCtx::FunctionArg),
421 MethodCall(ref call) => (&call.args[..], UnusedDelimsCtx::MethodArg),
422 Closure(ref closure)
423 if #[allow(non_exhaustive_omitted_patterns)] match closure.fn_decl.output {
FnRetTy::Default(_) => true,
_ => false,
}matches!(closure.fn_decl.output, FnRetTy::Default(_)) =>
424 {
425 (&[closure.body.clone()][..], UnusedDelimsCtx::ClosureBody)
426 }
427 _ => {
429 return;
430 }
431 };
432 if e.span.ctxt().outer_expn_data().call_site.from_expansion() {
437 return;
438 }
439 for arg in args_to_check {
440 self.check_unused_delims_expr(cx, arg, ctx, false, None, None, false);
441 }
442 return;
443 }
444 };
445 self.check_unused_delims_expr(
446 cx,
447 value,
448 ctx,
449 followed_by_block,
450 left_pos,
451 right_pos,
452 is_kw,
453 );
454 }
455
456 fn check_stmt(&mut self, cx: &EarlyContext<'_>, s: &ast::Stmt) {
457 match s.kind {
458 StmtKind::Let(ref local) if Self::LINT_EXPR_IN_PATTERN_MATCHING_CTX => {
459 if let Some((init, els)) = local.kind.init_else_opt() {
460 if els.is_some()
461 && let ExprKind::Paren(paren) = &init.kind
462 && !init.span.eq_ctxt(paren.span)
463 {
464 return;
475 }
476 let ctx = match els {
477 None => UnusedDelimsCtx::AssignedValue,
478 Some(_) => UnusedDelimsCtx::AssignedValueLetElse,
479 };
480 self.check_unused_delims_expr(cx, init, ctx, false, None, None, false);
481 }
482 }
483 StmtKind::Expr(ref expr) => {
484 self.check_unused_delims_expr(
485 cx,
486 expr,
487 UnusedDelimsCtx::BlockRetValue,
488 false,
489 None,
490 None,
491 false,
492 );
493 }
494 _ => {}
495 }
496 }
497
498 fn check_item(&mut self, cx: &EarlyContext<'_>, item: &ast::Item) {
499 use ast::ItemKind::*;
500
501 let expr = if let Const(box ast::ConstItem { rhs_kind, .. }) = &item.kind {
502 if let Some(e) = rhs_kind.expr() { e } else { return }
503 } else if let Static(box ast::StaticItem { expr: Some(expr), .. }) = &item.kind {
504 expr
505 } else {
506 return;
507 };
508 self.check_unused_delims_expr(
509 cx,
510 expr,
511 UnusedDelimsCtx::AssignedValue,
512 false,
513 None,
514 None,
515 false,
516 );
517 }
518}
519
520#[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! {
521 pub(super) UNUSED_PARENS,
537 Warn,
538 "`if`, `match`, `while` and `return` do not need parentheses"
539}
540
541#[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)]
542pub(crate) struct UnusedParens {
543 with_self_ty_parens: bool,
544 parens_in_cast_in_lt: Vec<ast::NodeId>,
547 in_no_bounds_pos: FxHashMap<ast::NodeId, NoBoundsException>,
550}
551
552enum NoBoundsException {
567 None,
569 OneBound,
572}
573
574impl ::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]);
575
576impl UnusedDelimLint for UnusedParens {
577 const DELIM_STR: &'static str = "parentheses";
578
579 const LINT_EXPR_IN_PATTERN_MATCHING_CTX: bool = true;
580
581 fn lint(&self) -> &'static Lint {
582 UNUSED_PARENS
583 }
584
585 fn check_unused_delims_expr(
586 &self,
587 cx: &EarlyContext<'_>,
588 value: &ast::Expr,
589 ctx: UnusedDelimsCtx,
590 followed_by_block: bool,
591 left_pos: Option<BytePos>,
592 right_pos: Option<BytePos>,
593 is_kw: bool,
594 ) {
595 match value.kind {
596 ast::ExprKind::Paren(ref inner) => {
597 if !Self::is_expr_delims_necessary(inner, ctx, followed_by_block)
598 && value.attrs.is_empty()
599 && !value.span.from_expansion()
600 && (ctx != UnusedDelimsCtx::LetScrutineeExpr
601 || !#[allow(non_exhaustive_omitted_patterns)] match inner.kind {
ast::ExprKind::Binary(rustc_span::source_map::Spanned { node, .. }, _, _)
if node.is_lazy() => true,
_ => false,
}matches!(inner.kind, ast::ExprKind::Binary(
602 rustc_span::source_map::Spanned { node, .. },
603 _,
604 _,
605 ) if node.is_lazy()))
606 && !((ctx == UnusedDelimsCtx::ReturnValue
607 || ctx == UnusedDelimsCtx::BreakValue)
608 && #[allow(non_exhaustive_omitted_patterns)] match inner.kind {
ast::ExprKind::Assign(_, _, _) => true,
_ => false,
}matches!(inner.kind, ast::ExprKind::Assign(_, _, _)))
609 {
610 self.emit_unused_delims_expr(cx, value, ctx, left_pos, right_pos, is_kw)
611 }
612 }
613 ast::ExprKind::Let(_, ref expr, _, _) => {
614 self.check_unused_delims_expr(
615 cx,
616 expr,
617 UnusedDelimsCtx::LetScrutineeExpr,
618 followed_by_block,
619 None,
620 None,
621 false,
622 );
623 }
624 _ => {}
625 }
626 }
627}
628
629impl UnusedParens {
630 fn check_unused_parens_pat(
631 &self,
632 cx: &EarlyContext<'_>,
633 value: &ast::Pat,
634 avoid_or: bool,
635 avoid_mut: bool,
636 keep_space: (bool, bool),
637 ) {
638 use ast::{BindingMode, PatKind};
639
640 if let PatKind::Paren(inner) = &value.kind {
641 match inner.kind {
642 PatKind::Range(..) => return,
647 PatKind::Guard(..) => return,
649 PatKind::Or(..) if avoid_or => return,
651 PatKind::Ident(BindingMode::MUT, ..) if avoid_mut => {
653 return;
654 }
655 _ => {}
657 }
658 let spans = if !value.span.from_expansion() {
659 inner
660 .span
661 .find_ancestor_inside(value.span)
662 .map(|inner| (value.span.with_hi(inner.lo()), value.span.with_lo(inner.hi())))
663 } else {
664 None
665 };
666 self.emit_unused_delims(cx, value.span, spans, "pattern", keep_space, false);
667 }
668 }
669
670 fn cast_followed_by_lt(&self, expr: &ast::Expr) -> Option<ast::NodeId> {
671 if let ExprKind::Binary(op, lhs, _rhs) = &expr.kind
672 && (op.node == ast::BinOpKind::Lt || op.node == ast::BinOpKind::Shl)
673 {
674 let mut cur = lhs;
675 while let ExprKind::Binary(_, _, rhs) = &cur.kind {
676 cur = rhs;
677 }
678
679 if let ExprKind::Cast(_, ty) = &cur.kind
680 && let ast::TyKind::Paren(_) = &ty.kind
681 {
682 return Some(ty.id);
683 }
684 }
685 None
686 }
687}
688
689impl EarlyLintPass for UnusedParens {
690 #[inline]
691 fn check_expr(&mut self, cx: &EarlyContext<'_>, e: &ast::Expr) {
692 if let Some(ty_id) = self.cast_followed_by_lt(e) {
693 self.parens_in_cast_in_lt.push(ty_id);
694 }
695
696 match e.kind {
697 ExprKind::Let(ref pat, _, _, _) | ExprKind::ForLoop { ref pat, .. } => {
698 self.check_unused_parens_pat(cx, pat, false, false, (true, true));
699 }
700 ExprKind::If(ref cond, ref block, ref else_)
704 if #[allow(non_exhaustive_omitted_patterns)] match cond.peel_parens().kind {
ExprKind::Let(..) => true,
_ => false,
}matches!(cond.peel_parens().kind, ExprKind::Let(..)) =>
705 {
706 self.check_unused_delims_expr(
707 cx,
708 cond.peel_parens(),
709 UnusedDelimsCtx::LetScrutineeExpr,
710 true,
711 None,
712 None,
713 true,
714 );
715 for stmt in &block.stmts {
716 <Self as UnusedDelimLint>::check_stmt(self, cx, stmt);
717 }
718 if let Some(e) = else_ {
719 <Self as UnusedDelimLint>::check_expr(self, cx, e);
720 }
721 return;
722 }
723 ExprKind::Match(ref _expr, ref arm, _) => {
724 for a in arm {
725 if let Some(body) = &a.body {
726 self.check_unused_delims_expr(
727 cx,
728 body,
729 UnusedDelimsCtx::MatchArmExpr,
730 false,
731 None,
732 None,
733 true,
734 );
735 }
736 }
737 }
738 _ => {}
739 }
740
741 <Self as UnusedDelimLint>::check_expr(self, cx, e)
742 }
743
744 fn check_expr_post(&mut self, _cx: &EarlyContext<'_>, e: &ast::Expr) {
745 if let Some(ty_id) = self.cast_followed_by_lt(e) {
746 let id = self
747 .parens_in_cast_in_lt
748 .pop()
749 .expect("check_expr and check_expr_post must balance");
750 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!(
751 id, ty_id,
752 "check_expr, check_ty, and check_expr_post are called, in that order, by the visitor"
753 );
754 }
755 }
756
757 fn check_pat(&mut self, cx: &EarlyContext<'_>, p: &ast::Pat) {
758 use ast::Mutability;
759 use ast::PatKind::*;
760 let keep_space = (false, false);
761 match &p.kind {
762 Paren(_)
764 | Missing | Wild | Never | Rest | Expr(..) | MacCall(..) | Range(..) | Ident(.., None)
766 | Path(..) | Err(_) => {},
767 TupleStruct(_, _, ps) | Tuple(ps) | Slice(ps) | Or(ps) => for p in ps {
769 self.check_unused_parens_pat(cx, p, false, false, keep_space);
770 },
771 Struct(_, _, fps, _) => for f in fps {
772 self.check_unused_parens_pat(cx, &f.pat, false, false, keep_space);
773 },
774 Ident(.., Some(p)) | Box(p) | Deref(p) | Guard(p, _) => self.check_unused_parens_pat(cx, p, true, false, keep_space),
776 Ref(p, _, m) => self.check_unused_parens_pat(cx, p, true, *m == Mutability::Not, keep_space),
780 }
781 }
782
783 fn check_stmt(&mut self, cx: &EarlyContext<'_>, s: &ast::Stmt) {
784 if let StmtKind::Let(ref local) = s.kind {
785 self.check_unused_parens_pat(cx, &local.pat, true, false, (true, false));
786 }
787
788 <Self as UnusedDelimLint>::check_stmt(self, cx, s)
789 }
790
791 fn check_param(&mut self, cx: &EarlyContext<'_>, param: &ast::Param) {
792 self.check_unused_parens_pat(cx, ¶m.pat, true, false, (false, false));
793 }
794
795 fn check_arm(&mut self, cx: &EarlyContext<'_>, arm: &ast::Arm) {
796 self.check_unused_parens_pat(cx, &arm.pat, false, false, (false, false));
797 }
798
799 fn check_ty(&mut self, cx: &EarlyContext<'_>, ty: &ast::Ty) {
800 if let ast::TyKind::Paren(_) = ty.kind
801 && Some(&ty.id) == self.parens_in_cast_in_lt.last()
802 {
803 return;
804 }
805 match &ty.kind {
806 ast::TyKind::Array(_, len) => {
807 self.check_unused_delims_expr(
808 cx,
809 &len.value,
810 UnusedDelimsCtx::ArrayLenExpr,
811 false,
812 None,
813 None,
814 false,
815 );
816 }
817 ast::TyKind::Paren(r) => {
818 let unused_parens = match &r.kind {
819 ast::TyKind::ImplTrait(_, bounds) | ast::TyKind::TraitObject(bounds, _) => {
820 match self.in_no_bounds_pos.get(&ty.id) {
821 Some(NoBoundsException::None) => false,
822 Some(NoBoundsException::OneBound) => bounds.len() <= 1,
823 None => true,
824 }
825 }
826 ast::TyKind::FnPtr(b) => {
827 !self.with_self_ty_parens || b.generic_params.is_empty()
828 }
829 _ => true,
830 };
831
832 if unused_parens {
833 let spans = (!ty.span.from_expansion())
834 .then(|| {
835 r.span
836 .find_ancestor_inside(ty.span)
837 .map(|r| (ty.span.with_hi(r.lo()), ty.span.with_lo(r.hi())))
838 })
839 .flatten();
840
841 self.emit_unused_delims(cx, ty.span, spans, "type", (false, false), false);
842 }
843
844 self.with_self_ty_parens = false;
845 }
846 ast::TyKind::Ref(_, mut_ty) | ast::TyKind::Ptr(mut_ty) => {
847 let own_constraint = self.in_no_bounds_pos.get(&ty.id);
850 let constraint = match own_constraint {
851 Some(NoBoundsException::None) => NoBoundsException::None,
852 Some(NoBoundsException::OneBound) => NoBoundsException::OneBound,
853 None => NoBoundsException::OneBound,
854 };
855 self.in_no_bounds_pos.insert(mut_ty.ty.id, constraint);
856 }
857 ast::TyKind::TraitObject(bounds, _) | ast::TyKind::ImplTrait(_, bounds) => {
858 for i in 0..bounds.len() {
859 let is_last = i == bounds.len() - 1;
860
861 if let ast::GenericBound::Trait(poly_trait_ref) = &bounds[i] {
862 let fn_with_explicit_ret_ty = if let [.., segment] =
863 &*poly_trait_ref.trait_ref.path.segments
864 && let Some(args) = segment.args.as_ref()
865 && let ast::GenericArgs::Parenthesized(paren_args) = &**args
866 && let ast::FnRetTy::Ty(ret_ty) = &paren_args.output
867 {
868 self.in_no_bounds_pos.insert(
869 ret_ty.id,
870 if is_last {
871 NoBoundsException::OneBound
872 } else {
873 NoBoundsException::None
874 },
875 );
876
877 true
878 } else {
879 false
880 };
881
882 let dyn2015_exception = cx.sess().psess.edition == Edition2015
887 && #[allow(non_exhaustive_omitted_patterns)] match ty.kind {
ast::TyKind::TraitObject(..) => true,
_ => false,
}matches!(ty.kind, ast::TyKind::TraitObject(..))
888 && i == 0
889 && poly_trait_ref
890 .trait_ref
891 .path
892 .segments
893 .first()
894 .map(|s| s.ident.name == kw::PathRoot)
895 .unwrap_or(false);
896
897 if let ast::Parens::Yes = poly_trait_ref.parens
898 && (is_last || !fn_with_explicit_ret_ty)
899 && !dyn2015_exception
900 {
901 let s = poly_trait_ref.span;
902 let spans = (!s.from_expansion()).then(|| {
903 (
904 s.with_hi(s.lo() + rustc_span::BytePos(1)),
905 s.with_lo(s.hi() - rustc_span::BytePos(1)),
906 )
907 });
908
909 self.emit_unused_delims(
910 cx,
911 poly_trait_ref.span,
912 spans,
913 "type",
914 (false, false),
915 false,
916 );
917 }
918 }
919 }
920 }
921 _ => {}
922 }
923 }
924
925 fn check_item(&mut self, cx: &EarlyContext<'_>, item: &ast::Item) {
926 <Self as UnusedDelimLint>::check_item(self, cx, item)
927 }
928
929 fn check_item_post(&mut self, _: &EarlyContext<'_>, _: &rustc_ast::Item) {
930 self.in_no_bounds_pos.clear();
931 }
932
933 fn enter_where_predicate(&mut self, _: &EarlyContext<'_>, pred: &ast::WherePredicate) {
934 use rustc_ast::{WhereBoundPredicate, WherePredicateKind};
935 if let WherePredicateKind::BoundPredicate(WhereBoundPredicate {
936 bounded_ty,
937 bound_generic_params,
938 ..
939 }) = &pred.kind
940 && let ast::TyKind::Paren(_) = &bounded_ty.kind
941 && bound_generic_params.is_empty()
942 {
943 self.with_self_ty_parens = true;
944 }
945 }
946
947 fn exit_where_predicate(&mut self, _: &EarlyContext<'_>, _: &ast::WherePredicate) {
948 if !!self.with_self_ty_parens {
::core::panicking::panic("assertion failed: !self.with_self_ty_parens")
};assert!(!self.with_self_ty_parens);
949 }
950}
951
952#[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! {
953 pub(super) UNUSED_BRACES,
971 Warn,
972 "unnecessary braces around an expression"
973}
974
975pub 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]);
976
977impl UnusedDelimLint for UnusedBraces {
978 const DELIM_STR: &'static str = "braces";
979
980 const LINT_EXPR_IN_PATTERN_MATCHING_CTX: bool = false;
981
982 fn lint(&self) -> &'static Lint {
983 UNUSED_BRACES
984 }
985
986 fn check_unused_delims_expr(
987 &self,
988 cx: &EarlyContext<'_>,
989 value: &ast::Expr,
990 ctx: UnusedDelimsCtx,
991 followed_by_block: bool,
992 left_pos: Option<BytePos>,
993 right_pos: Option<BytePos>,
994 is_kw: bool,
995 ) {
996 match value.kind {
997 ast::ExprKind::Block(ref inner, None)
998 if inner.rules == ast::BlockCheckMode::Default =>
999 {
1000 if let [stmt] = inner.stmts.as_slice()
1025 && let ast::StmtKind::Expr(ref expr) = stmt.kind
1026 && !Self::is_expr_delims_necessary(expr, ctx, followed_by_block)
1027 && (ctx != UnusedDelimsCtx::AnonConst
1028 || (#[allow(non_exhaustive_omitted_patterns)] match expr.kind {
ast::ExprKind::Lit(_) => true,
_ => false,
}matches!(expr.kind, ast::ExprKind::Lit(_))
1029 && !expr.span.from_expansion()))
1030 && ctx != UnusedDelimsCtx::ClosureBody
1031 && !cx.sess().source_map().is_multiline(value.span)
1032 && value.attrs.is_empty()
1033 && !value.span.from_expansion()
1034 && !inner.span.from_expansion()
1035 {
1036 self.emit_unused_delims_expr(cx, value, ctx, left_pos, right_pos, is_kw)
1037 }
1038 }
1039 ast::ExprKind::Let(_, ref expr, _, _) => {
1040 self.check_unused_delims_expr(
1041 cx,
1042 expr,
1043 UnusedDelimsCtx::LetScrutineeExpr,
1044 followed_by_block,
1045 None,
1046 None,
1047 false,
1048 );
1049 }
1050 _ => {}
1051 }
1052 }
1053}
1054
1055impl EarlyLintPass for UnusedBraces {
1056 fn check_stmt(&mut self, cx: &EarlyContext<'_>, s: &ast::Stmt) {
1057 <Self as UnusedDelimLint>::check_stmt(self, cx, s)
1058 }
1059
1060 #[inline]
1061 fn check_expr(&mut self, cx: &EarlyContext<'_>, e: &ast::Expr) {
1062 <Self as UnusedDelimLint>::check_expr(self, cx, e);
1063
1064 if let ExprKind::Repeat(_, ref anon_const) = e.kind {
1065 self.check_unused_delims_expr(
1066 cx,
1067 &anon_const.value,
1068 UnusedDelimsCtx::AnonConst,
1069 false,
1070 None,
1071 None,
1072 false,
1073 );
1074 }
1075 }
1076
1077 fn check_generic_arg(&mut self, cx: &EarlyContext<'_>, arg: &ast::GenericArg) {
1078 if let ast::GenericArg::Const(ct) = arg {
1079 self.check_unused_delims_expr(
1080 cx,
1081 &ct.value,
1082 UnusedDelimsCtx::AnonConst,
1083 false,
1084 None,
1085 None,
1086 false,
1087 );
1088 }
1089 }
1090
1091 fn check_variant(&mut self, cx: &EarlyContext<'_>, v: &ast::Variant) {
1092 if let Some(anon_const) = &v.disr_expr {
1093 self.check_unused_delims_expr(
1094 cx,
1095 &anon_const.value,
1096 UnusedDelimsCtx::AnonConst,
1097 false,
1098 None,
1099 None,
1100 false,
1101 );
1102 }
1103 }
1104
1105 fn check_ty(&mut self, cx: &EarlyContext<'_>, ty: &ast::Ty) {
1106 match ty.kind {
1107 ast::TyKind::Array(_, ref len) => {
1108 self.check_unused_delims_expr(
1109 cx,
1110 &len.value,
1111 UnusedDelimsCtx::ArrayLenExpr,
1112 false,
1113 None,
1114 None,
1115 false,
1116 );
1117 }
1118
1119 _ => {}
1120 }
1121 }
1122
1123 fn check_item(&mut self, cx: &EarlyContext<'_>, item: &ast::Item) {
1124 <Self as UnusedDelimLint>::check_item(self, cx, item)
1125 }
1126}
1127
1128#[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! {
1129 UNUSED_IMPORT_BRACES,
1154 Allow,
1155 "unnecessary braces around an imported item"
1156}
1157
1158pub 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]);
1159
1160impl UnusedImportBraces {
1161 fn check_use_tree(&self, cx: &EarlyContext<'_>, use_tree: &ast::UseTree, item: &ast::Item) {
1162 if let ast::UseTreeKind::Nested { ref items, .. } = use_tree.kind {
1163 for (tree, _) in items {
1165 self.check_use_tree(cx, tree, item);
1166 }
1167
1168 let [(tree, _)] = items.as_slice() else { return };
1170
1171 let node_name = match tree.kind {
1173 ast::UseTreeKind::Simple(rename) => {
1174 let orig_ident = tree.prefix.segments.last().unwrap().ident;
1175 if orig_ident.name == kw::SelfLower {
1176 return;
1177 }
1178 rename.unwrap_or(orig_ident).name
1179 }
1180 ast::UseTreeKind::Glob => sym::asterisk,
1181 ast::UseTreeKind::Nested { .. } => return,
1182 };
1183
1184 cx.emit_span_lint(
1185 UNUSED_IMPORT_BRACES,
1186 item.span,
1187 UnusedImportBracesDiag { node: node_name },
1188 );
1189 }
1190 }
1191}
1192
1193impl EarlyLintPass for UnusedImportBraces {
1194 fn check_item(&mut self, cx: &EarlyContext<'_>, item: &ast::Item) {
1195 if let ast::ItemKind::Use(ref use_tree) = item.kind {
1196 self.check_use_tree(cx, use_tree, item);
1197 }
1198 }
1199}
1200
1201#[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! {
1202 pub(super) UNUSED_ALLOCATION,
1221 Warn,
1222 "detects unnecessary allocations that can be eliminated"
1223}
1224
1225pub 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]);
1226
1227impl<'tcx> LateLintPass<'tcx> for UnusedAllocation {
1228 fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &hir::Expr<'_>) {
1229 match e.kind {
1230 hir::ExprKind::Call(path_expr, [_])
1231 if let hir::ExprKind::Path(qpath) = &path_expr.kind
1232 && let Some(did) = cx.qpath_res(qpath, path_expr.hir_id).opt_def_id()
1233 && cx.tcx.is_diagnostic_item(sym::box_new, did) => {}
1234 _ => return,
1235 }
1236
1237 for adj in cx.typeck_results().expr_adjustments(e) {
1238 if let adjustment::Adjust::Borrow(adjustment::AutoBorrow::Ref(m)) = adj.kind {
1239 if let ty::Ref(_, inner_ty, _) = adj.target.kind()
1240 && inner_ty.is_box()
1241 {
1242 continue;
1244 }
1245 match m {
1246 adjustment::AutoBorrowMutability::Not => {
1247 cx.emit_span_lint(UNUSED_ALLOCATION, e.span, UnusedAllocationDiag);
1248 }
1249 adjustment::AutoBorrowMutability::Mut { .. } => {
1250 cx.emit_span_lint(UNUSED_ALLOCATION, e.span, UnusedAllocationMutDiag);
1251 }
1252 };
1253 }
1254 }
1255 }
1256}