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, Unnormalized};
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
113#[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)]
115pub enum MustUsePath {
116 Def(Span, DefId, Option<Symbol>),
118 Boxed(Box<Self>),
119 Pinned(Box<Self>),
120 Opaque(Box<Self>),
121 TraitObject(Box<Self>),
122 TupleElement(Vec<(usize, Self)>),
123 Result(Box<Self>),
125 ControlFlow(Box<Self>),
127 Array(Box<Self>, u64),
128 Closure(Span),
130 Coroutine(Span),
132}
133
134x;#[instrument(skip(cx, expr), level = "debug", ret)]
137pub fn is_ty_must_use<'tcx>(
138 cx: &LateContext<'tcx>,
139 ty: Ty<'tcx>,
140 expr: &hir::Expr<'_>,
141) -> IsTyMustUse {
142 if ty.is_unit() {
143 return IsTyMustUse::Trivial;
144 }
145
146 let parent_mod_did = cx.tcx.parent_module(expr.hir_id).to_def_id();
147 let is_uninhabited =
148 |t: Ty<'tcx>| !t.is_inhabited_from(cx.tcx, parent_mod_did, cx.typing_env());
149
150 match *ty.kind() {
151 _ if is_uninhabited(ty) => IsTyMustUse::Trivial,
152 ty::Adt(..) if let Some(boxed) = ty.boxed_ty() => {
153 is_ty_must_use(cx, boxed, expr).map(|inner| MustUsePath::Boxed(Box::new(inner)))
154 }
155 ty::Adt(def, args) if cx.tcx.is_lang_item(def.did(), LangItem::Pin) => {
156 let pinned_ty = args.type_at(0);
157 is_ty_must_use(cx, pinned_ty, expr).map(|inner| MustUsePath::Pinned(Box::new(inner)))
158 }
159 ty::Adt(def, args)
161 if cx.tcx.is_diagnostic_item(sym::Result, def.did())
162 && is_uninhabited(args.type_at(1)) =>
163 {
164 let ok_ty = args.type_at(0);
165 is_ty_must_use(cx, ok_ty, expr).map(|path| MustUsePath::Result(Box::new(path)))
166 }
167 ty::Adt(def, args)
169 if cx.tcx.is_diagnostic_item(sym::ControlFlow, def.did())
170 && is_uninhabited(args.type_at(0)) =>
171 {
172 let continue_ty = args.type_at(1);
173 is_ty_must_use(cx, continue_ty, expr)
174 .map(|path| MustUsePath::ControlFlow(Box::new(path)))
175 }
176 ty::Adt(def, _) => {
177 is_def_must_use(cx, def.did(), expr.span).map_or(IsTyMustUse::No, IsTyMustUse::Yes)
178 }
179 ty::Alias(ty::AliasTy {
180 kind: ty::Opaque { def_id: def } | ty::Projection { def_id: def },
181 ..
182 }) => {
183 elaborate(
184 cx.tcx,
185 cx.tcx
186 .explicit_item_self_bounds(def)
187 .iter_identity_copied()
188 .map(Unnormalized::skip_norm_wip),
189 )
190 .filter_only_self()
192 .find_map(|(pred, _span)| {
193 if let ty::ClauseKind::Trait(ref poly_trait_predicate) = pred.kind().skip_binder() {
195 let def_id = poly_trait_predicate.trait_ref.def_id;
196
197 is_def_must_use(cx, def_id, expr.span)
198 } else {
199 None
200 }
201 })
202 .map(|inner| MustUsePath::Opaque(Box::new(inner)))
203 .map_or(IsTyMustUse::No, IsTyMustUse::Yes)
204 }
205 ty::Dynamic(binders, _) => binders
206 .iter()
207 .find_map(|predicate| {
208 if let ty::ExistentialPredicate::Trait(ref trait_ref) = predicate.skip_binder() {
209 let def_id = trait_ref.def_id;
210 is_def_must_use(cx, def_id, expr.span)
211 .map(|inner| MustUsePath::TraitObject(Box::new(inner)))
212 } else {
213 None
214 }
215 })
216 .map_or(IsTyMustUse::No, IsTyMustUse::Yes),
217 ty::Tuple(tys) => {
219 let elem_exprs = if let hir::ExprKind::Tup(elem_exprs) = expr.kind {
220 debug_assert_eq!(elem_exprs.len(), tys.len());
221 elem_exprs
222 } else {
223 &[]
224 };
225
226 let elem_exprs = elem_exprs.iter().chain(iter::repeat(expr));
228
229 let mut all_trivial = true;
230 let mut nested_must_use = Vec::new();
231
232 tys.iter().zip(elem_exprs).enumerate().for_each(|(i, (ty, expr))| {
233 let must_use = is_ty_must_use(cx, ty, expr);
234
235 all_trivial &= matches!(must_use, IsTyMustUse::Trivial);
236 if let IsTyMustUse::Yes(path) = must_use {
237 nested_must_use.push((i, path));
238 }
239 });
240
241 if all_trivial {
242 IsTyMustUse::Trivial
245 } else if !nested_must_use.is_empty() {
246 IsTyMustUse::Yes(MustUsePath::TupleElement(nested_must_use))
247 } else {
248 IsTyMustUse::No
249 }
250 }
251 ty::Array(ty, len) => match len.try_to_target_usize(cx.tcx) {
252 Some(0) | None => IsTyMustUse::No,
254 Some(len) => {
256 is_ty_must_use(cx, ty, expr).map(|inner| MustUsePath::Array(Box::new(inner), len))
257 }
258 },
259 ty::Closure(..) | ty::CoroutineClosure(..) => {
260 IsTyMustUse::Yes(MustUsePath::Closure(expr.span))
261 }
262 ty::Coroutine(def_id, ..) => {
263 if cx.tcx.coroutine_is_async(def_id)
265 && let Some(def_id) = cx.tcx.lang_items().future_trait()
266 {
267 IsTyMustUse::Yes(MustUsePath::Opaque(Box::new(
268 is_def_must_use(cx, def_id, expr.span)
269 .expect("future trait is marked as `#[must_use]`"),
270 )))
271 } else {
272 IsTyMustUse::Yes(MustUsePath::Coroutine(expr.span))
273 }
274 }
275 _ => IsTyMustUse::No,
276 }
277}
278
279impl<'tcx> LateLintPass<'tcx> for UnusedResults {
280 fn check_stmt(&mut self, cx: &LateContext<'_>, s: &hir::Stmt<'_>) {
281 let hir::StmtKind::Semi(mut expr) = s.kind else {
282 return;
283 };
284
285 let mut expr_is_from_block = false;
286 while let hir::ExprKind::Block(blk, ..) = expr.kind
287 && let hir::Block { expr: Some(e), .. } = blk
288 {
289 expr = e;
290 expr_is_from_block = true;
291 }
292
293 if let hir::ExprKind::Ret(..) = expr.kind {
294 return;
295 }
296
297 if let hir::ExprKind::Match(await_expr, _arms, hir::MatchSource::AwaitDesugar) = expr.kind
298 && let ty = cx.typeck_results().expr_ty(await_expr)
299 && let ty::Alias(ty::AliasTy { kind: ty::Opaque { def_id: future_def_id }, .. }) = ty.kind()
300 && cx.tcx.ty_is_opaque_future(ty)
301 && let async_fn_def_id = cx.tcx.parent(*future_def_id)
302 && #[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)
303 && cx.tcx.asyncness(async_fn_def_id).is_async()
305 && check_must_use_def(
306 cx,
307 async_fn_def_id,
308 expr.span,
309 "output of future returned by ",
310 "",
311 expr_is_from_block,
312 )
313 {
314 return;
317 }
318
319 let ty = cx.typeck_results().expr_ty(expr);
320
321 let must_use_result = is_ty_must_use(cx, ty, expr);
322 let type_lint_emitted_or_trivial = match must_use_result {
323 IsTyMustUse::Yes(path) => {
324 emit_must_use_untranslated(cx, &path, "", "", 1, false, expr_is_from_block);
325 true
326 }
327 IsTyMustUse::Trivial => true,
328 IsTyMustUse::No => false,
329 };
330
331 let fn_warned = check_fn_must_use(cx, expr, expr_is_from_block);
332
333 if !fn_warned && type_lint_emitted_or_trivial {
334 return;
337 }
338
339 let must_use_op = match expr.kind {
340 hir::ExprKind::Binary(bin_op, ..) => match bin_op.node {
344 hir::BinOpKind::Eq
345 | hir::BinOpKind::Lt
346 | hir::BinOpKind::Le
347 | hir::BinOpKind::Ne
348 | hir::BinOpKind::Ge
349 | hir::BinOpKind::Gt => Some("comparison"),
350 hir::BinOpKind::Add
351 | hir::BinOpKind::Sub
352 | hir::BinOpKind::Div
353 | hir::BinOpKind::Mul
354 | hir::BinOpKind::Rem => Some("arithmetic operation"),
355 hir::BinOpKind::And | hir::BinOpKind::Or => Some("logical operation"),
356 hir::BinOpKind::BitXor
357 | hir::BinOpKind::BitAnd
358 | hir::BinOpKind::BitOr
359 | hir::BinOpKind::Shl
360 | hir::BinOpKind::Shr => Some("bitwise operation"),
361 },
362 hir::ExprKind::AddrOf(..) => Some("borrow"),
363 hir::ExprKind::OffsetOf(..) => Some("`offset_of` call"),
364 hir::ExprKind::Unary(..) => Some("unary operation"),
365 hir::ExprKind::ConstBlock(block) => {
367 let body = cx.tcx.hir_body(block.body);
368 if let hir::ExprKind::Block(block, _) = body.value.kind
369 && let Some(expr) = block.expr
370 && let hir::ExprKind::OffsetOf(..) = expr.kind
371 {
372 Some("`offset_of` call")
373 } else {
374 None
375 }
376 }
377 _ => None,
378 };
379
380 let op_warned = match must_use_op {
381 Some(must_use_op) => {
382 let span = expr.span.find_ancestor_not_from_macro().unwrap_or(expr.span);
383 cx.emit_span_lint(
384 UNUSED_MUST_USE,
385 expr.span,
386 UnusedOp {
387 op: must_use_op,
388 label: expr.span,
389 suggestion: if expr_is_from_block {
390 UnusedOpSuggestion::BlockTailExpr {
391 before_span: span.shrink_to_lo(),
392 after_span: span.shrink_to_hi(),
393 }
394 } else {
395 UnusedOpSuggestion::NormalExpr { span: span.shrink_to_lo() }
396 },
397 },
398 );
399 true
400 }
401 None => false,
402 };
403
404 if !(type_lint_emitted_or_trivial || fn_warned || op_warned) {
406 cx.emit_span_lint(UNUSED_RESULTS, s.span, UnusedResult { ty });
407 }
408 }
409}
410
411fn check_fn_must_use(cx: &LateContext<'_>, expr: &hir::Expr<'_>, expr_is_from_block: bool) -> bool {
414 let maybe_def_id = match expr.kind {
415 hir::ExprKind::Call(callee, _) => {
416 if let hir::ExprKind::Path(ref qpath) = callee.kind
417 && let Res::Def(DefKind::Fn | DefKind::AssocFn, def_id) =
420 cx.qpath_res(qpath, callee.hir_id)
421 {
422 Some(def_id)
423 } else {
424 None
425 }
426 }
427 hir::ExprKind::MethodCall(..) => cx.typeck_results().type_dependent_def_id(expr.hir_id),
428 _ => None,
429 };
430
431 match maybe_def_id {
432 Some(def_id) => {
433 check_must_use_def(cx, def_id, expr.span, "return value of ", "", expr_is_from_block)
434 }
435 None => false,
436 }
437}
438
439fn is_def_must_use(cx: &LateContext<'_>, def_id: DefId, span: Span) -> Option<MustUsePath> {
440 {
{
'done:
{
for i in ::rustc_hir::attrs::HasAttrs::get_attrs(def_id, &cx.tcx)
{
#[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)
442 .map(|reason| MustUsePath::Def(span, def_id, *reason))
443}
444
445fn check_must_use_def(
447 cx: &LateContext<'_>,
448 def_id: DefId,
449 span: Span,
450 descr_pre_path: &str,
451 descr_post_path: &str,
452 expr_is_from_block: bool,
453) -> bool {
454 is_def_must_use(cx, def_id, span)
455 .map(|must_use_path| {
456 emit_must_use_untranslated(
457 cx,
458 &must_use_path,
459 descr_pre_path,
460 descr_post_path,
461 1,
462 false,
463 expr_is_from_block,
464 )
465 })
466 .is_some()
467}
468
469#[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(469u32),
::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")]
470fn emit_must_use_untranslated(
471 cx: &LateContext<'_>,
472 path: &MustUsePath,
473 descr_pre: &str,
474 descr_post: &str,
475 plural_len: usize,
476 is_inner: bool,
477 expr_is_from_block: bool,
478) {
479 let plural_suffix = pluralize!(plural_len);
480
481 match path {
482 MustUsePath::Boxed(path) => {
483 let descr_pre = &format!("{descr_pre}boxed ");
484 emit_must_use_untranslated(
485 cx,
486 path,
487 descr_pre,
488 descr_post,
489 plural_len,
490 true,
491 expr_is_from_block,
492 );
493 }
494 MustUsePath::Pinned(path) => {
495 let descr_pre = &format!("{descr_pre}pinned ");
496 emit_must_use_untranslated(
497 cx,
498 path,
499 descr_pre,
500 descr_post,
501 plural_len,
502 true,
503 expr_is_from_block,
504 );
505 }
506 MustUsePath::Opaque(path) => {
507 let descr_pre = &format!("{descr_pre}implementer{plural_suffix} of ");
508 emit_must_use_untranslated(
509 cx,
510 path,
511 descr_pre,
512 descr_post,
513 plural_len,
514 true,
515 expr_is_from_block,
516 );
517 }
518 MustUsePath::TraitObject(path) => {
519 let descr_post = &format!(" trait object{plural_suffix}{descr_post}");
520 emit_must_use_untranslated(
521 cx,
522 path,
523 descr_pre,
524 descr_post,
525 plural_len,
526 true,
527 expr_is_from_block,
528 );
529 }
530 MustUsePath::TupleElement(elems) => {
531 for (index, path) in elems {
532 let descr_post = &format!(" in tuple element {index}");
533 emit_must_use_untranslated(
534 cx,
535 path,
536 descr_pre,
537 descr_post,
538 plural_len,
539 true,
540 expr_is_from_block,
541 );
542 }
543 }
544 MustUsePath::Result(path) => {
545 let descr_post = &format!(" in a `Result` with an uninhabited error{descr_post}");
546 emit_must_use_untranslated(
547 cx,
548 path,
549 descr_pre,
550 descr_post,
551 plural_len,
552 true,
553 expr_is_from_block,
554 );
555 }
556 MustUsePath::ControlFlow(path) => {
557 let descr_post = &format!(" in a `ControlFlow` with an uninhabited break {descr_post}");
558 emit_must_use_untranslated(
559 cx,
560 path,
561 descr_pre,
562 descr_post,
563 plural_len,
564 true,
565 expr_is_from_block,
566 );
567 }
568 MustUsePath::Array(path, len) => {
569 let descr_pre = &format!("{descr_pre}array{plural_suffix} of ");
570 emit_must_use_untranslated(
571 cx,
572 path,
573 descr_pre,
574 descr_post,
575 plural_len.saturating_add(usize::try_from(*len).unwrap_or(usize::MAX)),
576 true,
577 expr_is_from_block,
578 );
579 }
580 MustUsePath::Closure(span) => {
581 cx.emit_span_lint(
582 UNUSED_MUST_USE,
583 *span,
584 UnusedClosure { count: plural_len, pre: descr_pre, post: descr_post },
585 );
586 }
587 MustUsePath::Coroutine(span) => {
588 cx.emit_span_lint(
589 UNUSED_MUST_USE,
590 *span,
591 UnusedCoroutine { count: plural_len, pre: descr_pre, post: descr_post },
592 );
593 }
594 MustUsePath::Def(span, def_id, reason) => {
595 let ancenstor_span = span.find_ancestor_not_from_macro().unwrap_or(*span);
596 let is_redundant_let_ignore = cx
597 .sess()
598 .source_map()
599 .span_to_prev_source(ancenstor_span)
600 .ok()
601 .map(|prev| prev.trim_end().ends_with("let _ ="))
602 .unwrap_or(false);
603 let suggestion_span = if is_redundant_let_ignore { *span } else { ancenstor_span };
604 cx.emit_span_lint(
605 UNUSED_MUST_USE,
606 ancenstor_span,
607 UnusedDef {
608 pre: descr_pre,
609 post: descr_post,
610 cx,
611 def_id: *def_id,
612 note: *reason,
613 suggestion: (!is_inner).then_some(if expr_is_from_block {
614 UnusedDefSuggestion::BlockTailExpr {
615 before_span: suggestion_span.shrink_to_lo(),
616 after_span: suggestion_span.shrink_to_hi(),
617 }
618 } else {
619 UnusedDefSuggestion::NormalExpr { span: suggestion_span.shrink_to_lo() }
620 }),
621 },
622 );
623 }
624 }
625}