1use std::iter;
2
3use rustc_errors::pluralize;
4use rustc_hir::def::{DefKind, Res};
5use rustc_hir::def_id::DefId;
6use rustc_hir::{self as hir, LangItem, find_attr};
7use rustc_infer::traits::util::elaborate;
8use rustc_middle::ty::{self, Ty};
9use rustc_session::{declare_lint, declare_lint_pass};
10use rustc_span::{Span, Symbol, sym};
11use tracing::instrument;
12
13use crate::lints::{
14 UnusedClosure, UnusedCoroutine, UnusedDef, UnusedDefSuggestion, UnusedOp, UnusedOpSuggestion,
15 UnusedResult,
16};
17use crate::{LateContext, LateLintPass, LintContext};
18
19#[doc =
r" The `unused_must_use` lint detects unused result of a type flagged as"]
#[doc = r" `#[must_use]`."]
#[doc = r""]
#[doc = r" ### Example"]
#[doc = r""]
#[doc = r" ```rust"]
#[doc = r" fn returns_result() -> Result<(), ()> {"]
#[doc = r" Ok(())"]
#[doc = r" }"]
#[doc = r""]
#[doc = r" fn main() {"]
#[doc = r" returns_result();"]
#[doc = r" }"]
#[doc = r" ```"]
#[doc = r""]
#[doc = r" {{produces}}"]
#[doc = r""]
#[doc = r" ### Explanation"]
#[doc = r""]
#[doc =
r" The `#[must_use]` attribute is an indicator that it is a mistake to"]
#[doc = r" ignore the value. See [the reference] for more details."]
#[doc = r""]
#[doc =
r" [the reference]: https://doc.rust-lang.org/reference/attributes/diagnostics.html#the-must_use-attribute"]
pub static UNUSED_MUST_USE: &::rustc_lint_defs::Lint =
&::rustc_lint_defs::Lint {
name: "UNUSED_MUST_USE",
default_level: ::rustc_lint_defs::Warn,
desc: "unused result of a type flagged as `#[must_use]`",
is_externally_loaded: false,
report_in_external_macro: true,
..::rustc_lint_defs::Lint::default_fields_for_macro()
};declare_lint! {
20 pub UNUSED_MUST_USE,
44 Warn,
45 "unused result of a type flagged as `#[must_use]`",
46 report_in_external_macro
47}
48
49#[doc = r" The `unused_results` lint checks for the unused result of an"]
#[doc = r" expression in a statement."]
#[doc = r""]
#[doc = r" ### Example"]
#[doc = r""]
#[doc = r" ```rust,compile_fail"]
#[doc = r" #![deny(unused_results)]"]
#[doc = r" fn foo<T>() -> T { panic!() }"]
#[doc = r""]
#[doc = r" fn main() {"]
#[doc = r" foo::<usize>();"]
#[doc = r" }"]
#[doc = r" ```"]
#[doc = r""]
#[doc = r" {{produces}}"]
#[doc = r""]
#[doc = r" ### Explanation"]
#[doc = r""]
#[doc =
r" Ignoring the return value of a function may indicate a mistake. In"]
#[doc =
r" cases were it is almost certain that the result should be used, it is"]
#[doc =
r" recommended to annotate the function with the [`must_use` attribute]."]
#[doc =
r" Failure to use such a return value will trigger the [`unused_must_use`"]
#[doc = r" lint] which is warn-by-default. The `unused_results` lint is"]
#[doc = r" essentially the same, but triggers for *all* return values."]
#[doc = r""]
#[doc =
r#" This lint is "allow" by default because it can be noisy, and may not be"#]
#[doc =
r" an actual problem. For example, calling the `remove` method of a `Vec`"]
#[doc =
r" or `HashMap` returns the previous value, which you may not care about."]
#[doc =
r" Using this lint would require explicitly ignoring or discarding such"]
#[doc = r" values."]
#[doc = r""]
#[doc =
r" [`must_use` attribute]: https://doc.rust-lang.org/reference/attributes/diagnostics.html#the-must_use-attribute"]
#[doc = r" [`unused_must_use` lint]: warn-by-default.html#unused-must-use"]
pub static UNUSED_RESULTS: &::rustc_lint_defs::Lint =
&::rustc_lint_defs::Lint {
name: "UNUSED_RESULTS",
default_level: ::rustc_lint_defs::Allow,
desc: "unused result of an expression in a statement",
is_externally_loaded: false,
..::rustc_lint_defs::Lint::default_fields_for_macro()
};declare_lint! {
50 pub UNUSED_RESULTS,
84 Allow,
85 "unused result of an expression in a statement"
86}
87
88pub struct UnusedResults;
#[automatically_derived]
impl ::core::marker::Copy for UnusedResults { }
#[automatically_derived]
#[doc(hidden)]
unsafe impl ::core::clone::TrivialClone for UnusedResults { }
#[automatically_derived]
impl ::core::clone::Clone for UnusedResults {
#[inline]
fn clone(&self) -> UnusedResults { *self }
}
impl ::rustc_lint_defs::LintPass for UnusedResults {
fn name(&self) -> &'static str { "UnusedResults" }
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_MUST_USE, UNUSED_RESULTS]))
}
}
impl UnusedResults {
#[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_MUST_USE, UNUSED_RESULTS]))
}
}declare_lint_pass!(UnusedResults => [UNUSED_MUST_USE, UNUSED_RESULTS]);
89
90#[derive(#[automatically_derived]
impl ::core::fmt::Debug for IsTyMustUse {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
match self {
IsTyMustUse::Yes(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f, "Yes",
&__self_0),
IsTyMustUse::No => ::core::fmt::Formatter::write_str(f, "No"),
IsTyMustUse::Trivial =>
::core::fmt::Formatter::write_str(f, "Trivial"),
}
}
}Debug)]
92pub enum IsTyMustUse {
93 Yes(MustUsePath),
96 No,
99 Trivial,
102}
103
104impl IsTyMustUse {
105 fn map(self, f: impl FnOnce(MustUsePath) -> MustUsePath) -> Self {
106 match self {
107 Self::Yes(must_use_path) => Self::Yes(f(must_use_path)),
108 _ => self,
109 }
110 }
111
112 fn yes(self) -> Option<MustUsePath> {
113 match self {
114 Self::Yes(must_use_path) => Some(must_use_path),
115 _ => None,
116 }
117 }
118}
119
120#[derive(#[automatically_derived]
impl ::core::fmt::Debug for MustUsePath {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
match self {
MustUsePath::Def(__self_0, __self_1, __self_2) =>
::core::fmt::Formatter::debug_tuple_field3_finish(f, "Def",
__self_0, __self_1, &__self_2),
MustUsePath::Boxed(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f, "Boxed",
&__self_0),
MustUsePath::Pinned(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f, "Pinned",
&__self_0),
MustUsePath::Opaque(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f, "Opaque",
&__self_0),
MustUsePath::TraitObject(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"TraitObject", &__self_0),
MustUsePath::TupleElement(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"TupleElement", &__self_0),
MustUsePath::Result(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f, "Result",
&__self_0),
MustUsePath::ControlFlow(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"ControlFlow", &__self_0),
MustUsePath::Array(__self_0, __self_1) =>
::core::fmt::Formatter::debug_tuple_field2_finish(f, "Array",
__self_0, &__self_1),
MustUsePath::Closure(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"Closure", &__self_0),
MustUsePath::Coroutine(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"Coroutine", &__self_0),
}
}
}Debug)]
122pub enum MustUsePath {
123 Def(Span, DefId, Option<Symbol>),
125 Boxed(Box<Self>),
126 Pinned(Box<Self>),
127 Opaque(Box<Self>),
128 TraitObject(Box<Self>),
129 TupleElement(Vec<(usize, Self)>),
130 Result(Box<Self>),
132 ControlFlow(Box<Self>),
134 Array(Box<Self>, u64),
135 Closure(Span),
137 Coroutine(Span),
139}
140
141x;#[instrument(skip(cx, expr), level = "debug", ret)]
150pub fn is_ty_must_use<'tcx>(
151 cx: &LateContext<'tcx>,
152 ty: Ty<'tcx>,
153 expr: &hir::Expr<'_>,
154 span: Span,
155 simplify_uninhabited: bool,
156) -> IsTyMustUse {
157 if ty.is_unit() {
158 return IsTyMustUse::Trivial;
159 }
160
161 let parent_mod_did = cx.tcx.parent_module(expr.hir_id).to_def_id();
162 let is_uninhabited =
163 |t: Ty<'tcx>| !t.is_inhabited_from(cx.tcx, parent_mod_did, cx.typing_env());
164
165 match *ty.kind() {
166 _ if is_uninhabited(ty) => IsTyMustUse::Trivial,
167 ty::Adt(..) if let Some(boxed) = ty.boxed_ty() => {
168 is_ty_must_use(cx, boxed, expr, span, simplify_uninhabited)
169 .map(|inner| MustUsePath::Boxed(Box::new(inner)))
170 }
171 ty::Adt(def, args) if cx.tcx.is_lang_item(def.did(), LangItem::Pin) => {
172 let pinned_ty = args.type_at(0);
173 is_ty_must_use(cx, pinned_ty, expr, span, simplify_uninhabited)
174 .map(|inner| MustUsePath::Pinned(Box::new(inner)))
175 }
176 ty::Adt(def, args)
178 if simplify_uninhabited
179 && cx.tcx.is_diagnostic_item(sym::Result, def.did())
180 && is_uninhabited(args.type_at(1)) =>
181 {
182 let ok_ty = args.type_at(0);
183 is_ty_must_use(cx, ok_ty, expr, span, simplify_uninhabited)
184 .map(|path| MustUsePath::Result(Box::new(path)))
185 }
186 ty::Adt(def, args)
188 if simplify_uninhabited
189 && cx.tcx.is_diagnostic_item(sym::ControlFlow, def.did())
190 && is_uninhabited(args.type_at(0)) =>
191 {
192 let continue_ty = args.type_at(1);
193 is_ty_must_use(cx, continue_ty, expr, span, simplify_uninhabited)
194 .map(|path| MustUsePath::ControlFlow(Box::new(path)))
195 }
196 ty::Adt(def, args)
198 if cx.tcx.is_diagnostic_item(sym::Result, def.did())
199 && args.type_at(0).is_unit()
200 && is_uninhabited(args.type_at(1)) =>
201 {
202 IsTyMustUse::Trivial
203 }
204 ty::Adt(def, args)
206 if cx.tcx.is_diagnostic_item(sym::ControlFlow, def.did())
207 && args.type_at(1).is_unit()
208 && is_uninhabited(args.type_at(0)) =>
209 {
210 IsTyMustUse::Trivial
211 }
212 ty::Adt(def, _) => {
213 is_def_must_use(cx, def.did(), span).map_or(IsTyMustUse::No, IsTyMustUse::Yes)
214 }
215 ty::Alias(ty::Opaque | ty::Projection, ty::AliasTy { def_id: def, .. }) => {
216 elaborate(cx.tcx, cx.tcx.explicit_item_self_bounds(def).iter_identity_copied())
217 .filter_only_self()
219 .find_map(|(pred, _span)| {
220 if let ty::ClauseKind::Trait(ref poly_trait_predicate) =
222 pred.kind().skip_binder()
223 {
224 let def_id = poly_trait_predicate.trait_ref.def_id;
225
226 is_def_must_use(cx, def_id, span)
227 } else {
228 None
229 }
230 })
231 .map(|inner| MustUsePath::Opaque(Box::new(inner)))
232 .map_or(IsTyMustUse::No, IsTyMustUse::Yes)
233 }
234 ty::Dynamic(binders, _) => binders
235 .iter()
236 .find_map(|predicate| {
237 if let ty::ExistentialPredicate::Trait(ref trait_ref) = predicate.skip_binder() {
238 let def_id = trait_ref.def_id;
239 is_def_must_use(cx, def_id, span)
240 .map(|inner| MustUsePath::TraitObject(Box::new(inner)))
241 } else {
242 None
243 }
244 })
245 .map_or(IsTyMustUse::No, IsTyMustUse::Yes),
246 ty::Tuple(tys) => {
248 let elem_exprs = if let hir::ExprKind::Tup(elem_exprs) = expr.kind {
249 debug_assert_eq!(elem_exprs.len(), tys.len());
250 elem_exprs
251 } else {
252 &[]
253 };
254
255 let elem_exprs = elem_exprs.iter().chain(iter::repeat(expr));
257
258 let nested_must_use = tys
259 .iter()
260 .zip(elem_exprs)
261 .enumerate()
262 .filter_map(|(i, (ty, expr))| {
263 is_ty_must_use(cx, ty, expr, expr.span, simplify_uninhabited)
264 .yes()
265 .map(|path| (i, path))
266 })
267 .collect::<Vec<_>>();
268
269 if !nested_must_use.is_empty() {
270 IsTyMustUse::Yes(MustUsePath::TupleElement(nested_must_use))
271 } else {
272 IsTyMustUse::No
273 }
274 }
275 ty::Array(ty, len) => match len.try_to_target_usize(cx.tcx) {
276 Some(0) | None => IsTyMustUse::No,
278 Some(len) => is_ty_must_use(cx, ty, expr, span, simplify_uninhabited)
280 .map(|inner| MustUsePath::Array(Box::new(inner), len)),
281 },
282 ty::Closure(..) | ty::CoroutineClosure(..) => IsTyMustUse::Yes(MustUsePath::Closure(span)),
283 ty::Coroutine(def_id, ..) => {
284 if cx.tcx.coroutine_is_async(def_id)
286 && let Some(def_id) = cx.tcx.lang_items().future_trait()
287 {
288 IsTyMustUse::Yes(MustUsePath::Opaque(Box::new(
289 is_def_must_use(cx, def_id, span)
290 .expect("future trait is marked as `#[must_use]`"),
291 )))
292 } else {
293 IsTyMustUse::Yes(MustUsePath::Coroutine(span))
294 }
295 }
296 _ => IsTyMustUse::No,
297 }
298}
299
300impl<'tcx> LateLintPass<'tcx> for UnusedResults {
301 fn check_stmt(&mut self, cx: &LateContext<'_>, s: &hir::Stmt<'_>) {
302 let hir::StmtKind::Semi(mut expr) = s.kind else {
303 return;
304 };
305
306 let mut expr_is_from_block = false;
307 while let hir::ExprKind::Block(blk, ..) = expr.kind
308 && let hir::Block { expr: Some(e), .. } = blk
309 {
310 expr = e;
311 expr_is_from_block = true;
312 }
313
314 if let hir::ExprKind::Ret(..) = expr.kind {
315 return;
316 }
317
318 if let hir::ExprKind::Match(await_expr, _arms, hir::MatchSource::AwaitDesugar) = expr.kind
319 && let ty = cx.typeck_results().expr_ty(await_expr)
320 && let ty::Alias(ty::Opaque, ty::AliasTy { def_id: future_def_id, .. }) = ty.kind()
321 && cx.tcx.ty_is_opaque_future(ty)
322 && let async_fn_def_id = cx.tcx.parent(*future_def_id)
323 && #[allow(non_exhaustive_omitted_patterns)] match cx.tcx.def_kind(async_fn_def_id)
{
DefKind::Fn | DefKind::AssocFn => true,
_ => false,
}matches!(cx.tcx.def_kind(async_fn_def_id), DefKind::Fn | DefKind::AssocFn)
324 && cx.tcx.asyncness(async_fn_def_id).is_async()
326 && check_must_use_def(
327 cx,
328 async_fn_def_id,
329 expr.span,
330 "output of future returned by ",
331 "",
332 expr_is_from_block,
333 )
334 {
335 return;
338 }
339
340 let ty = cx.typeck_results().expr_ty(expr);
341
342 let must_use_result = is_ty_must_use(cx, ty, expr, expr.span, false);
343 let type_lint_emitted_or_trivial = match must_use_result {
344 IsTyMustUse::Yes(path) => {
345 emit_must_use_untranslated(cx, &path, "", "", 1, false, expr_is_from_block);
346 true
347 }
348 IsTyMustUse::Trivial => true,
349 IsTyMustUse::No => false,
350 };
351
352 let fn_warned = check_fn_must_use(cx, expr, expr_is_from_block);
353
354 if !fn_warned && type_lint_emitted_or_trivial {
355 return;
358 }
359
360 let must_use_op = match expr.kind {
361 hir::ExprKind::Binary(bin_op, ..) => match bin_op.node {
365 hir::BinOpKind::Eq
366 | hir::BinOpKind::Lt
367 | hir::BinOpKind::Le
368 | hir::BinOpKind::Ne
369 | hir::BinOpKind::Ge
370 | hir::BinOpKind::Gt => Some("comparison"),
371 hir::BinOpKind::Add
372 | hir::BinOpKind::Sub
373 | hir::BinOpKind::Div
374 | hir::BinOpKind::Mul
375 | hir::BinOpKind::Rem => Some("arithmetic operation"),
376 hir::BinOpKind::And | hir::BinOpKind::Or => Some("logical operation"),
377 hir::BinOpKind::BitXor
378 | hir::BinOpKind::BitAnd
379 | hir::BinOpKind::BitOr
380 | hir::BinOpKind::Shl
381 | hir::BinOpKind::Shr => Some("bitwise operation"),
382 },
383 hir::ExprKind::AddrOf(..) => Some("borrow"),
384 hir::ExprKind::OffsetOf(..) => Some("`offset_of` call"),
385 hir::ExprKind::Unary(..) => Some("unary operation"),
386 hir::ExprKind::ConstBlock(block) => {
388 let body = cx.tcx.hir_body(block.body);
389 if let hir::ExprKind::Block(block, _) = body.value.kind
390 && let Some(expr) = block.expr
391 && let hir::ExprKind::OffsetOf(..) = expr.kind
392 {
393 Some("`offset_of` call")
394 } else {
395 None
396 }
397 }
398 _ => None,
399 };
400
401 let op_warned = match must_use_op {
402 Some(must_use_op) => {
403 let span = expr.span.find_ancestor_not_from_macro().unwrap_or(expr.span);
404 cx.emit_span_lint(
405 UNUSED_MUST_USE,
406 expr.span,
407 UnusedOp {
408 op: must_use_op,
409 label: expr.span,
410 suggestion: if expr_is_from_block {
411 UnusedOpSuggestion::BlockTailExpr {
412 before_span: span.shrink_to_lo(),
413 after_span: span.shrink_to_hi(),
414 }
415 } else {
416 UnusedOpSuggestion::NormalExpr { span: span.shrink_to_lo() }
417 },
418 },
419 );
420 true
421 }
422 None => false,
423 };
424
425 if !(type_lint_emitted_or_trivial || fn_warned || op_warned) {
427 cx.emit_span_lint(UNUSED_RESULTS, s.span, UnusedResult { ty });
428 }
429 }
430}
431
432fn check_fn_must_use(cx: &LateContext<'_>, expr: &hir::Expr<'_>, expr_is_from_block: bool) -> bool {
435 let maybe_def_id = match expr.kind {
436 hir::ExprKind::Call(callee, _) => {
437 if let hir::ExprKind::Path(ref qpath) = callee.kind
438 && let Res::Def(DefKind::Fn | DefKind::AssocFn, def_id) =
441 cx.qpath_res(qpath, callee.hir_id)
442 {
443 Some(def_id)
444 } else {
445 None
446 }
447 }
448 hir::ExprKind::MethodCall(..) => cx.typeck_results().type_dependent_def_id(expr.hir_id),
449 _ => None,
450 };
451
452 match maybe_def_id {
453 Some(def_id) => {
454 check_must_use_def(cx, def_id, expr.span, "return value of ", "", expr_is_from_block)
455 }
456 None => false,
457 }
458}
459
460fn is_def_must_use(cx: &LateContext<'_>, def_id: DefId, span: Span) -> Option<MustUsePath> {
461 {
#[allow(deprecated)]
{
{
'done:
{
for i in cx.tcx.get_all_attrs(def_id) {
#[allow(unused_imports)]
use rustc_hir::attrs::AttributeKind::*;
let i: &rustc_hir::Attribute = i;
match i {
rustc_hir::Attribute::Parsed(MustUse { reason, .. }) => {
break 'done Some(reason);
}
rustc_hir::Attribute::Unparsed(..) =>
{}
#[deny(unreachable_patterns)]
_ => {}
}
}
None
}
}
}
}find_attr!(cx.tcx, def_id, MustUse { reason, .. } => reason)
463 .map(|reason| MustUsePath::Def(span, def_id, *reason))
464}
465
466fn check_must_use_def(
468 cx: &LateContext<'_>,
469 def_id: DefId,
470 span: Span,
471 descr_pre_path: &str,
472 descr_post_path: &str,
473 expr_is_from_block: bool,
474) -> bool {
475 is_def_must_use(cx, def_id, span)
476 .map(|must_use_path| {
477 emit_must_use_untranslated(
478 cx,
479 &must_use_path,
480 descr_pre_path,
481 descr_post_path,
482 1,
483 false,
484 expr_is_from_block,
485 )
486 })
487 .is_some()
488}
489
490#[allow(clippy :: suspicious_else_formatting)]
{
let __tracing_attr_span;
let __tracing_attr_guard;
if ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() ||
{ false } {
__tracing_attr_span =
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("emit_must_use_untranslated",
"rustc_lint::unused::must_use", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_lint/src/unused/must_use.rs"),
::tracing_core::__macro_support::Option::Some(490u32),
::tracing_core::__macro_support::Option::Some("rustc_lint::unused::must_use"),
::tracing_core::field::FieldSet::new(&["path", "descr_pre",
"descr_post", "plural_len", "is_inner",
"expr_is_from_block"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::SPAN)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let mut interest = ::tracing::subscriber::Interest::never();
if ::tracing::Level::DEBUG <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{ interest = __CALLSITE.interest(); !interest.is_never() }
&&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest) {
let meta = __CALLSITE.metadata();
::tracing::Span::new(meta,
&{
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = meta.fields().iter();
meta.fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&::tracing::field::debug(&path)
as &dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&descr_pre as
&dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&descr_post as
&dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&plural_len as
&dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&is_inner as
&dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&expr_is_from_block
as &dyn Value))])
})
} else {
let span =
::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
{};
span
}
};
__tracing_attr_guard = __tracing_attr_span.enter();
}
#[warn(clippy :: suspicious_else_formatting)]
{
#[allow(unknown_lints, unreachable_code, clippy ::
diverging_sub_expression, clippy :: empty_loop, clippy ::
let_unit_value, clippy :: let_with_type_underscore, clippy ::
needless_return, clippy :: unreachable)]
if false {
let __tracing_attr_fake_return: () = loop {};
return __tracing_attr_fake_return;
}
{
let plural_suffix = if plural_len == 1 { "" } else { "s" };
match path {
MustUsePath::Boxed(path) => {
let descr_pre =
&::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}boxed ", descr_pre))
});
emit_must_use_untranslated(cx, path, descr_pre, descr_post,
plural_len, true, expr_is_from_block);
}
MustUsePath::Pinned(path) => {
let descr_pre =
&::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}pinned ", descr_pre))
});
emit_must_use_untranslated(cx, path, descr_pre, descr_post,
plural_len, true, expr_is_from_block);
}
MustUsePath::Opaque(path) => {
let descr_pre =
&::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}implementer{1} of ",
descr_pre, plural_suffix))
});
emit_must_use_untranslated(cx, path, descr_pre, descr_post,
plural_len, true, expr_is_from_block);
}
MustUsePath::TraitObject(path) => {
let descr_post =
&::alloc::__export::must_use({
::alloc::fmt::format(format_args!(" trait object{0}{1}",
plural_suffix, descr_post))
});
emit_must_use_untranslated(cx, path, descr_pre, descr_post,
plural_len, true, expr_is_from_block);
}
MustUsePath::TupleElement(elems) => {
for (index, path) in elems {
let descr_post =
&::alloc::__export::must_use({
::alloc::fmt::format(format_args!(" in tuple element {0}",
index))
});
emit_must_use_untranslated(cx, path, descr_pre, descr_post,
plural_len, true, expr_is_from_block);
}
}
MustUsePath::Result(path) => {
let descr_post =
&::alloc::__export::must_use({
::alloc::fmt::format(format_args!(" in a `Result` with an uninhabited error{0}",
descr_post))
});
emit_must_use_untranslated(cx, path, descr_pre, descr_post,
plural_len, true, expr_is_from_block);
}
MustUsePath::ControlFlow(path) => {
let descr_post =
&::alloc::__export::must_use({
::alloc::fmt::format(format_args!(" in a `ControlFlow` with an uninhabited break {0}",
descr_post))
});
emit_must_use_untranslated(cx, path, descr_pre, descr_post,
plural_len, true, expr_is_from_block);
}
MustUsePath::Array(path, len) => {
let descr_pre =
&::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}array{1} of ",
descr_pre, plural_suffix))
});
emit_must_use_untranslated(cx, path, descr_pre, descr_post,
plural_len.saturating_add(usize::try_from(*len).unwrap_or(usize::MAX)),
true, expr_is_from_block);
}
MustUsePath::Closure(span) => {
cx.emit_span_lint(UNUSED_MUST_USE, *span,
UnusedClosure {
count: plural_len,
pre: descr_pre,
post: descr_post,
});
}
MustUsePath::Coroutine(span) => {
cx.emit_span_lint(UNUSED_MUST_USE, *span,
UnusedCoroutine {
count: plural_len,
pre: descr_pre,
post: descr_post,
});
}
MustUsePath::Def(span, def_id, reason) => {
let ancenstor_span =
span.find_ancestor_not_from_macro().unwrap_or(*span);
let is_redundant_let_ignore =
cx.sess().source_map().span_to_prev_source(ancenstor_span).ok().map(|prev|
prev.trim_end().ends_with("let _ =")).unwrap_or(false);
let suggestion_span =
if is_redundant_let_ignore {
*span
} else { ancenstor_span };
cx.emit_span_lint(UNUSED_MUST_USE, ancenstor_span,
UnusedDef {
pre: descr_pre,
post: descr_post,
cx,
def_id: *def_id,
note: *reason,
suggestion: (!is_inner).then_some(if expr_is_from_block {
UnusedDefSuggestion::BlockTailExpr {
before_span: suggestion_span.shrink_to_lo(),
after_span: suggestion_span.shrink_to_hi(),
}
} else {
UnusedDefSuggestion::NormalExpr {
span: suggestion_span.shrink_to_lo(),
}
}),
});
}
}
}
}
}#[instrument(skip(cx), level = "debug")]
491fn emit_must_use_untranslated(
492 cx: &LateContext<'_>,
493 path: &MustUsePath,
494 descr_pre: &str,
495 descr_post: &str,
496 plural_len: usize,
497 is_inner: bool,
498 expr_is_from_block: bool,
499) {
500 let plural_suffix = pluralize!(plural_len);
501
502 match path {
503 MustUsePath::Boxed(path) => {
504 let descr_pre = &format!("{descr_pre}boxed ");
505 emit_must_use_untranslated(
506 cx,
507 path,
508 descr_pre,
509 descr_post,
510 plural_len,
511 true,
512 expr_is_from_block,
513 );
514 }
515 MustUsePath::Pinned(path) => {
516 let descr_pre = &format!("{descr_pre}pinned ");
517 emit_must_use_untranslated(
518 cx,
519 path,
520 descr_pre,
521 descr_post,
522 plural_len,
523 true,
524 expr_is_from_block,
525 );
526 }
527 MustUsePath::Opaque(path) => {
528 let descr_pre = &format!("{descr_pre}implementer{plural_suffix} of ");
529 emit_must_use_untranslated(
530 cx,
531 path,
532 descr_pre,
533 descr_post,
534 plural_len,
535 true,
536 expr_is_from_block,
537 );
538 }
539 MustUsePath::TraitObject(path) => {
540 let descr_post = &format!(" trait object{plural_suffix}{descr_post}");
541 emit_must_use_untranslated(
542 cx,
543 path,
544 descr_pre,
545 descr_post,
546 plural_len,
547 true,
548 expr_is_from_block,
549 );
550 }
551 MustUsePath::TupleElement(elems) => {
552 for (index, path) in elems {
553 let descr_post = &format!(" in tuple element {index}");
554 emit_must_use_untranslated(
555 cx,
556 path,
557 descr_pre,
558 descr_post,
559 plural_len,
560 true,
561 expr_is_from_block,
562 );
563 }
564 }
565 MustUsePath::Result(path) => {
566 let descr_post = &format!(" in a `Result` with an uninhabited error{descr_post}");
567 emit_must_use_untranslated(
568 cx,
569 path,
570 descr_pre,
571 descr_post,
572 plural_len,
573 true,
574 expr_is_from_block,
575 );
576 }
577 MustUsePath::ControlFlow(path) => {
578 let descr_post = &format!(" in a `ControlFlow` with an uninhabited break {descr_post}");
579 emit_must_use_untranslated(
580 cx,
581 path,
582 descr_pre,
583 descr_post,
584 plural_len,
585 true,
586 expr_is_from_block,
587 );
588 }
589 MustUsePath::Array(path, len) => {
590 let descr_pre = &format!("{descr_pre}array{plural_suffix} of ");
591 emit_must_use_untranslated(
592 cx,
593 path,
594 descr_pre,
595 descr_post,
596 plural_len.saturating_add(usize::try_from(*len).unwrap_or(usize::MAX)),
597 true,
598 expr_is_from_block,
599 );
600 }
601 MustUsePath::Closure(span) => {
602 cx.emit_span_lint(
603 UNUSED_MUST_USE,
604 *span,
605 UnusedClosure { count: plural_len, pre: descr_pre, post: descr_post },
606 );
607 }
608 MustUsePath::Coroutine(span) => {
609 cx.emit_span_lint(
610 UNUSED_MUST_USE,
611 *span,
612 UnusedCoroutine { count: plural_len, pre: descr_pre, post: descr_post },
613 );
614 }
615 MustUsePath::Def(span, def_id, reason) => {
616 let ancenstor_span = span.find_ancestor_not_from_macro().unwrap_or(*span);
617 let is_redundant_let_ignore = cx
618 .sess()
619 .source_map()
620 .span_to_prev_source(ancenstor_span)
621 .ok()
622 .map(|prev| prev.trim_end().ends_with("let _ ="))
623 .unwrap_or(false);
624 let suggestion_span = if is_redundant_let_ignore { *span } else { ancenstor_span };
625 cx.emit_span_lint(
626 UNUSED_MUST_USE,
627 ancenstor_span,
628 UnusedDef {
629 pre: descr_pre,
630 post: descr_post,
631 cx,
632 def_id: *def_id,
633 note: *reason,
634 suggestion: (!is_inner).then_some(if expr_is_from_block {
635 UnusedDefSuggestion::BlockTailExpr {
636 before_span: suggestion_span.shrink_to_lo(),
637 after_span: suggestion_span.shrink_to_hi(),
638 }
639 } else {
640 UnusedDefSuggestion::NormalExpr { span: suggestion_span.shrink_to_lo() }
641 }),
642 },
643 );
644 }
645 }
646}