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(box ast::ConstItem { rhs_kind, .. }) = &item.kind {
503 if let Some(e) = rhs_kind.expr() { e } else { return }
504 } else if let Static(box ast::StaticItem { expr: Some(expr), .. }) = &item.kind {
505 expr
506 } else {
507 return;
508 };
509 self.check_unused_delims_expr(
510 cx,
511 expr,
512 UnusedDelimsCtx::AssignedValue,
513 false,
514 None,
515 None,
516 false,
517 );
518 }
519}
520
521#[doc =
r" The `unused_parens` lint detects `if`, `match`, `while` and `return`"]
#[doc = r" with parentheses; they do not need them."]
#[doc = r""]
#[doc = r" ### Examples"]
#[doc = r""]
#[doc = r" ```rust"]
#[doc = r" if(true) {}"]
#[doc = r" ```"]
#[doc = r""]
#[doc = r" {{produces}}"]
#[doc = r""]
#[doc = r" ### Explanation"]
#[doc = r""]
#[doc =
r" The parentheses are not needed, and should be removed. This is the"]
#[doc = r" preferred style for writing these expressions."]
pub(super) static UNUSED_PARENS: &::rustc_lint_defs::Lint =
&::rustc_lint_defs::Lint {
name: "UNUSED_PARENS",
default_level: ::rustc_lint_defs::Warn,
desc: "`if`, `match`, `while` and `return` do not need parentheses",
is_externally_loaded: false,
..::rustc_lint_defs::Lint::default_fields_for_macro()
};declare_lint! {
522 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 | Wild | Never | Rest | Expr(..) | MacCall(..) | Range(..) | Ident(.., None)
767 | Path(..) | Err(_) => {},
768 TupleStruct(_, _, ps) | Tuple(ps) | Slice(ps) | Or(ps) => for p in ps {
770 self.check_unused_parens_pat(cx, p, false, false, keep_space);
771 },
772 Struct(_, _, fps, _) => for f in fps {
773 self.check_unused_parens_pat(cx, &f.pat, false, false, keep_space);
774 },
775 Ident(.., Some(p)) | Box(p) | Deref(p) | Guard(p, _) => self.check_unused_parens_pat(cx, p, true, false, keep_space),
777 Ref(p, _, m) => self.check_unused_parens_pat(cx, p, true, *m == Mutability::Not, keep_space),
781 }
782 }
783
784 fn check_stmt(&mut self, cx: &EarlyContext<'_>, s: &ast::Stmt) {
785 if let StmtKind::Let(ref local) = s.kind {
786 self.check_unused_parens_pat(cx, &local.pat, true, false, (true, false));
787 }
788
789 <Self as UnusedDelimLint>::check_stmt(self, cx, s)
790 }
791
792 fn check_param(&mut self, cx: &EarlyContext<'_>, param: &ast::Param) {
793 self.check_unused_parens_pat(cx, ¶m.pat, true, false, (false, false));
794 }
795
796 fn check_arm(&mut self, cx: &EarlyContext<'_>, arm: &ast::Arm) {
797 self.check_unused_parens_pat(cx, &arm.pat, false, false, (false, false));
798 }
799
800 fn check_ty(&mut self, cx: &EarlyContext<'_>, ty: &ast::Ty) {
801 if let ast::TyKind::Paren(_) = ty.kind
802 && Some(&ty.id) == self.parens_in_cast_in_lt.last()
803 {
804 return;
805 }
806 match &ty.kind {
807 ast::TyKind::Array(_, len) => {
808 self.check_unused_delims_expr(
809 cx,
810 &len.value,
811 UnusedDelimsCtx::ArrayLenExpr,
812 false,
813 None,
814 None,
815 false,
816 );
817 }
818 ast::TyKind::Paren(r) => {
819 let unused_parens = match &r.kind {
820 ast::TyKind::ImplTrait(_, bounds) | ast::TyKind::TraitObject(bounds, _) => {
821 match self.in_no_bounds_pos.get(&ty.id) {
822 Some(NoBoundsException::None) => false,
823 Some(NoBoundsException::OneBound) => bounds.len() <= 1,
824 None => true,
825 }
826 }
827 ast::TyKind::FnPtr(b) => {
828 !self.with_self_ty_parens || b.generic_params.is_empty()
829 }
830 _ => true,
831 };
832
833 if unused_parens {
834 let spans = (!ty.span.from_expansion())
835 .then(|| {
836 r.span
837 .find_ancestor_inside(ty.span)
838 .map(|r| (ty.span.with_hi(r.lo()), ty.span.with_lo(r.hi())))
839 })
840 .flatten();
841
842 self.emit_unused_delims(cx, ty.span, spans, "type", (false, false), false);
843 }
844
845 self.with_self_ty_parens = false;
846 }
847 ast::TyKind::Ref(_, mut_ty) | ast::TyKind::Ptr(mut_ty) => {
848 let own_constraint = self.in_no_bounds_pos.get(&ty.id);
851 let constraint = match own_constraint {
852 Some(NoBoundsException::None) => NoBoundsException::None,
853 Some(NoBoundsException::OneBound) => NoBoundsException::OneBound,
854 None => NoBoundsException::OneBound,
855 };
856 self.in_no_bounds_pos.insert(mut_ty.ty.id, constraint);
857 }
858 ast::TyKind::TraitObject(bounds, _) | ast::TyKind::ImplTrait(_, bounds) => {
859 for i in 0..bounds.len() {
860 let is_last = i == bounds.len() - 1;
861
862 if let ast::GenericBound::Trait(poly_trait_ref) = &bounds[i] {
863 let fn_with_explicit_ret_ty = if let [.., segment] =
864 &*poly_trait_ref.trait_ref.path.segments
865 && let Some(args) = segment.args.as_ref()
866 && let ast::GenericArgs::Parenthesized(paren_args) = &**args
867 && let ast::FnRetTy::Ty(ret_ty) = &paren_args.output
868 {
869 self.in_no_bounds_pos.insert(
870 ret_ty.id,
871 if is_last {
872 NoBoundsException::OneBound
873 } else {
874 NoBoundsException::None
875 },
876 );
877
878 true
879 } else {
880 false
881 };
882
883 let dyn2015_exception = cx.sess().psess.edition == Edition2015
888 && #[allow(non_exhaustive_omitted_patterns)] match ty.kind {
ast::TyKind::TraitObject(..) => true,
_ => false,
}matches!(ty.kind, ast::TyKind::TraitObject(..))
889 && i == 0
890 && poly_trait_ref
891 .trait_ref
892 .path
893 .segments
894 .first()
895 .map(|s| s.ident.name == kw::PathRoot)
896 .unwrap_or(false);
897
898 if let ast::Parens::Yes = poly_trait_ref.parens
899 && (is_last || !fn_with_explicit_ret_ty)
900 && !dyn2015_exception
901 {
902 let s = poly_trait_ref.span;
903 let spans = (!s.from_expansion()).then(|| {
904 (
905 s.with_hi(s.lo() + rustc_span::BytePos(1)),
906 s.with_lo(s.hi() - rustc_span::BytePos(1)),
907 )
908 });
909
910 self.emit_unused_delims(
911 cx,
912 poly_trait_ref.span,
913 spans,
914 "type",
915 (false, false),
916 false,
917 );
918 }
919 }
920 }
921 }
922 _ => {}
923 }
924 }
925
926 fn check_item(&mut self, cx: &EarlyContext<'_>, item: &ast::Item) {
927 <Self as UnusedDelimLint>::check_item(self, cx, item)
928 }
929
930 fn check_item_post(&mut self, _: &EarlyContext<'_>, _: &rustc_ast::Item) {
931 self.in_no_bounds_pos.clear();
932 }
933
934 fn enter_where_predicate(&mut self, _: &EarlyContext<'_>, pred: &ast::WherePredicate) {
935 use rustc_ast::{WhereBoundPredicate, WherePredicateKind};
936 if let WherePredicateKind::BoundPredicate(WhereBoundPredicate {
937 bounded_ty,
938 bound_generic_params,
939 ..
940 }) = &pred.kind
941 && let ast::TyKind::Paren(_) = &bounded_ty.kind
942 && bound_generic_params.is_empty()
943 {
944 self.with_self_ty_parens = true;
945 }
946 }
947
948 fn exit_where_predicate(&mut self, _: &EarlyContext<'_>, _: &ast::WherePredicate) {
949 if !!self.with_self_ty_parens {
::core::panicking::panic("assertion failed: !self.with_self_ty_parens")
};assert!(!self.with_self_ty_parens);
950 }
951}
952
953#[doc = r" The `unused_braces` lint detects unnecessary braces around an"]
#[doc = r" expression."]
#[doc = r""]
#[doc = r" ### Example"]
#[doc = r""]
#[doc = r" ```rust"]
#[doc = r" if { true } {"]
#[doc = r" // ..."]
#[doc = r" }"]
#[doc = r" ```"]
#[doc = r""]
#[doc = r" {{produces}}"]
#[doc = r""]
#[doc = r" ### Explanation"]
#[doc = r""]
#[doc = r" The braces are not needed, and should be removed. This is the"]
#[doc = r" preferred style for writing these expressions."]
pub(super) static UNUSED_BRACES: &::rustc_lint_defs::Lint =
&::rustc_lint_defs::Lint {
name: "UNUSED_BRACES",
default_level: ::rustc_lint_defs::Warn,
desc: "unnecessary braces around an expression",
is_externally_loaded: false,
..::rustc_lint_defs::Lint::default_fields_for_macro()
};declare_lint! {
954 pub(super) UNUSED_BRACES,
972 Warn,
973 "unnecessary braces around an expression"
974}
975
976pub struct UnusedBraces;
#[automatically_derived]
impl ::core::marker::Copy for UnusedBraces { }
#[automatically_derived]
#[doc(hidden)]
unsafe impl ::core::clone::TrivialClone for UnusedBraces { }
#[automatically_derived]
impl ::core::clone::Clone for UnusedBraces {
#[inline]
fn clone(&self) -> UnusedBraces { *self }
}
impl ::rustc_lint_defs::LintPass for UnusedBraces {
fn name(&self) -> &'static str { "UnusedBraces" }
fn get_lints(&self) -> ::rustc_lint_defs::LintVec {
::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[UNUSED_BRACES]))
}
}
impl UnusedBraces {
#[allow(unused)]
pub fn lint_vec() -> ::rustc_lint_defs::LintVec {
::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[UNUSED_BRACES]))
}
}declare_lint_pass!(UnusedBraces => [UNUSED_BRACES]);
977
978impl UnusedDelimLint for UnusedBraces {
979 const DELIM_STR: &'static str = "braces";
980
981 const LINT_EXPR_IN_PATTERN_MATCHING_CTX: bool = false;
982
983 fn lint(&self) -> &'static Lint {
984 UNUSED_BRACES
985 }
986
987 fn check_unused_delims_expr(
988 &self,
989 cx: &EarlyContext<'_>,
990 value: &ast::Expr,
991 ctx: UnusedDelimsCtx,
992 followed_by_block: bool,
993 left_pos: Option<BytePos>,
994 right_pos: Option<BytePos>,
995 is_kw: bool,
996 ) {
997 match value.kind {
998 ast::ExprKind::Block(ref inner, None)
999 if inner.rules == ast::BlockCheckMode::Default =>
1000 {
1001 if let [stmt] = inner.stmts.as_slice()
1026 && let ast::StmtKind::Expr(ref expr) = stmt.kind
1027 && !Self::is_expr_delims_necessary(expr, ctx, followed_by_block)
1028 && (ctx != UnusedDelimsCtx::AnonConst
1029 || (#[allow(non_exhaustive_omitted_patterns)] match expr.kind {
ast::ExprKind::Lit(_) => true,
_ => false,
}matches!(expr.kind, ast::ExprKind::Lit(_))
1030 && !expr.span.from_expansion()))
1031 && ctx != UnusedDelimsCtx::ClosureBody
1032 && !cx.sess().source_map().is_multiline(value.span)
1033 && value.attrs.is_empty()
1034 && !value.span.from_expansion()
1035 && !inner.span.from_expansion()
1036 {
1037 self.emit_unused_delims_expr(cx, value, ctx, left_pos, right_pos, is_kw)
1038 }
1039 }
1040 ast::ExprKind::Let(_, ref expr, _, _) => {
1041 self.check_unused_delims_expr(
1042 cx,
1043 expr,
1044 UnusedDelimsCtx::LetScrutineeExpr,
1045 followed_by_block,
1046 None,
1047 None,
1048 false,
1049 );
1050 }
1051 _ => {}
1052 }
1053 }
1054}
1055
1056impl EarlyLintPass for UnusedBraces {
1057 fn check_stmt(&mut self, cx: &EarlyContext<'_>, s: &ast::Stmt) {
1058 <Self as UnusedDelimLint>::check_stmt(self, cx, s)
1059 }
1060
1061 #[inline]
1062 fn check_expr(&mut self, cx: &EarlyContext<'_>, e: &ast::Expr) {
1063 <Self as UnusedDelimLint>::check_expr(self, cx, e);
1064
1065 if let ExprKind::Repeat(_, ref anon_const) = e.kind {
1066 self.check_unused_delims_expr(
1067 cx,
1068 &anon_const.value,
1069 UnusedDelimsCtx::AnonConst,
1070 false,
1071 None,
1072 None,
1073 false,
1074 );
1075 }
1076 }
1077
1078 fn check_generic_arg(&mut self, cx: &EarlyContext<'_>, arg: &ast::GenericArg) {
1079 if let ast::GenericArg::Const(ct) = arg {
1080 self.check_unused_delims_expr(
1081 cx,
1082 &ct.value,
1083 UnusedDelimsCtx::AnonConst,
1084 false,
1085 None,
1086 None,
1087 false,
1088 );
1089 }
1090 }
1091
1092 fn check_variant(&mut self, cx: &EarlyContext<'_>, v: &ast::Variant) {
1093 if let Some(anon_const) = &v.disr_expr {
1094 self.check_unused_delims_expr(
1095 cx,
1096 &anon_const.value,
1097 UnusedDelimsCtx::AnonConst,
1098 false,
1099 None,
1100 None,
1101 false,
1102 );
1103 }
1104 }
1105
1106 fn check_ty(&mut self, cx: &EarlyContext<'_>, ty: &ast::Ty) {
1107 match ty.kind {
1108 ast::TyKind::Array(_, ref len) => {
1109 self.check_unused_delims_expr(
1110 cx,
1111 &len.value,
1112 UnusedDelimsCtx::ArrayLenExpr,
1113 false,
1114 None,
1115 None,
1116 false,
1117 );
1118 }
1119
1120 _ => {}
1121 }
1122 }
1123
1124 fn check_item(&mut self, cx: &EarlyContext<'_>, item: &ast::Item) {
1125 <Self as UnusedDelimLint>::check_item(self, cx, item)
1126 }
1127}
1128
1129#[doc =
r" The `unused_import_braces` lint catches unnecessary braces around an"]
#[doc = r" imported item."]
#[doc = r""]
#[doc = r" ### Example"]
#[doc = r""]
#[doc = r" ```rust,compile_fail"]
#[doc = r" #![deny(unused_import_braces)]"]
#[doc = r" use test::{A};"]
#[doc = r""]
#[doc = r" pub mod test {"]
#[doc = r" pub struct A;"]
#[doc = r" }"]
#[doc = r" # fn main() {}"]
#[doc = r" ```"]
#[doc = r""]
#[doc = r" {{produces}}"]
#[doc = r""]
#[doc = r" ### Explanation"]
#[doc = r""]
#[doc =
r" If there is only a single item, then remove the braces (`use test::A;`"]
#[doc = r" for example)."]
#[doc = r""]
#[doc = r#" This lint is "allow" by default because it is only enforcing a"#]
#[doc = r" stylistic choice."]
static UNUSED_IMPORT_BRACES: &::rustc_lint_defs::Lint =
&::rustc_lint_defs::Lint {
name: "UNUSED_IMPORT_BRACES",
default_level: ::rustc_lint_defs::Allow,
desc: "unnecessary braces around an imported item",
is_externally_loaded: false,
..::rustc_lint_defs::Lint::default_fields_for_macro()
};declare_lint! {
1130 UNUSED_IMPORT_BRACES,
1155 Allow,
1156 "unnecessary braces around an imported item"
1157}
1158
1159pub struct UnusedImportBraces;
#[automatically_derived]
impl ::core::marker::Copy for UnusedImportBraces { }
#[automatically_derived]
#[doc(hidden)]
unsafe impl ::core::clone::TrivialClone for UnusedImportBraces { }
#[automatically_derived]
impl ::core::clone::Clone for UnusedImportBraces {
#[inline]
fn clone(&self) -> UnusedImportBraces { *self }
}
impl ::rustc_lint_defs::LintPass for UnusedImportBraces {
fn name(&self) -> &'static str { "UnusedImportBraces" }
fn get_lints(&self) -> ::rustc_lint_defs::LintVec {
::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[UNUSED_IMPORT_BRACES]))
}
}
impl UnusedImportBraces {
#[allow(unused)]
pub fn lint_vec() -> ::rustc_lint_defs::LintVec {
::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[UNUSED_IMPORT_BRACES]))
}
}declare_lint_pass!(UnusedImportBraces => [UNUSED_IMPORT_BRACES]);
1160
1161impl UnusedImportBraces {
1162 fn check_use_tree(&self, cx: &EarlyContext<'_>, use_tree: &ast::UseTree, item: &ast::Item) {
1163 if let ast::UseTreeKind::Nested { ref items, .. } = use_tree.kind {
1164 for (tree, _) in items {
1166 self.check_use_tree(cx, tree, item);
1167 }
1168
1169 let [(tree, _)] = items.as_slice() else { return };
1171
1172 let node_name = match tree.kind {
1174 ast::UseTreeKind::Simple(rename) => {
1175 let orig_ident = tree.prefix.segments.last().unwrap().ident;
1176 if orig_ident.name == kw::SelfLower {
1177 return;
1178 }
1179 rename.unwrap_or(orig_ident).name
1180 }
1181 ast::UseTreeKind::Glob => sym::asterisk,
1182 ast::UseTreeKind::Nested { .. } => return,
1183 };
1184
1185 cx.emit_span_lint(
1186 UNUSED_IMPORT_BRACES,
1187 item.span,
1188 UnusedImportBracesDiag { node: node_name },
1189 );
1190 }
1191 }
1192}
1193
1194impl EarlyLintPass for UnusedImportBraces {
1195 fn check_item(&mut self, cx: &EarlyContext<'_>, item: &ast::Item) {
1196 if let ast::ItemKind::Use(ref use_tree) = item.kind {
1197 self.check_use_tree(cx, use_tree, item);
1198 }
1199 }
1200}
1201
1202#[doc =
r" The `unused_allocation` lint detects unnecessary allocations that can"]
#[doc = r" be eliminated."]
#[doc = r""]
#[doc = r" ### Example"]
#[doc = r""]
#[doc = r" ```rust"]
#[doc = r" fn main() {"]
#[doc = r" let a = Box::new([1, 2, 3]).len();"]
#[doc = r" }"]
#[doc = r" ```"]
#[doc = r""]
#[doc = r" {{produces}}"]
#[doc = r""]
#[doc = r" ### Explanation"]
#[doc = r""]
#[doc =
r" When a `box` expression is immediately coerced to a reference, then"]
#[doc =
r" the allocation is unnecessary, and a reference (using `&` or `&mut`)"]
#[doc = r" should be used instead to avoid the allocation."]
pub(super) static UNUSED_ALLOCATION: &::rustc_lint_defs::Lint =
&::rustc_lint_defs::Lint {
name: "UNUSED_ALLOCATION",
default_level: ::rustc_lint_defs::Warn,
desc: "detects unnecessary allocations that can be eliminated",
is_externally_loaded: false,
..::rustc_lint_defs::Lint::default_fields_for_macro()
};declare_lint! {
1203 pub(super) UNUSED_ALLOCATION,
1222 Warn,
1223 "detects unnecessary allocations that can be eliminated"
1224}
1225
1226pub struct UnusedAllocation;
#[automatically_derived]
impl ::core::marker::Copy for UnusedAllocation { }
#[automatically_derived]
#[doc(hidden)]
unsafe impl ::core::clone::TrivialClone for UnusedAllocation { }
#[automatically_derived]
impl ::core::clone::Clone for UnusedAllocation {
#[inline]
fn clone(&self) -> UnusedAllocation { *self }
}
impl ::rustc_lint_defs::LintPass for UnusedAllocation {
fn name(&self) -> &'static str { "UnusedAllocation" }
fn get_lints(&self) -> ::rustc_lint_defs::LintVec {
::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[UNUSED_ALLOCATION]))
}
}
impl UnusedAllocation {
#[allow(unused)]
pub fn lint_vec() -> ::rustc_lint_defs::LintVec {
::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[UNUSED_ALLOCATION]))
}
}declare_lint_pass!(UnusedAllocation => [UNUSED_ALLOCATION]);
1227
1228impl<'tcx> LateLintPass<'tcx> for UnusedAllocation {
1229 fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &hir::Expr<'_>) {
1230 match e.kind {
1231 hir::ExprKind::Call(path_expr, [_])
1232 if let hir::ExprKind::Path(qpath) = &path_expr.kind
1233 && let Some(did) = cx.qpath_res(qpath, path_expr.hir_id).opt_def_id()
1234 && cx.tcx.is_diagnostic_item(sym::box_new, did) => {}
1235 _ => return,
1236 }
1237
1238 for adj in cx.typeck_results().expr_adjustments(e) {
1239 if let adjustment::Adjust::Borrow(adjustment::AutoBorrow::Ref(m)) = adj.kind {
1240 if let ty::Ref(_, inner_ty, _) = adj.target.kind()
1241 && inner_ty.is_box()
1242 {
1243 continue;
1245 }
1246 match m {
1247 adjustment::AutoBorrowMutability::Not => {
1248 cx.emit_span_lint(UNUSED_ALLOCATION, e.span, UnusedAllocationDiag);
1249 }
1250 adjustment::AutoBorrowMutability::Mut { .. } => {
1251 cx.emit_span_lint(UNUSED_ALLOCATION, e.span, UnusedAllocationMutDiag);
1252 }
1253 };
1254 }
1255 }
1256 }
1257}