1use core::cmp::min;
3use core::iter;
4
5use hir::def_id::LocalDefId;
6use rustc_ast::util::parser::ExprPrecedence;
7use rustc_data_structures::packed::Pu128;
8use rustc_errors::{Applicability, Diag, MultiSpan, listify};
9use rustc_hir::def::{CtorKind, CtorOf, DefKind, Res};
10use rustc_hir::lang_items::LangItem;
11use rustc_hir::{
12 self as hir, Arm, CoroutineDesugaring, CoroutineKind, CoroutineSource, Expr, ExprKind,
13 GenericBound, HirId, Node, PatExpr, PatExprKind, Path, QPath, Stmt, StmtKind, TyKind,
14 WherePredicateKind, expr_needs_parens, is_range_literal,
15};
16use rustc_hir_analysis::hir_ty_lowering::HirTyLowerer;
17use rustc_hir_analysis::suggest_impl_trait;
18use rustc_middle::middle::stability::EvalResult;
19use rustc_middle::span_bug;
20use rustc_middle::ty::print::with_no_trimmed_paths;
21use rustc_middle::ty::{
22 self, Article, Binder, IsSuggestable, Ty, TyCtxt, TypeVisitableExt, Upcast,
23 suggest_constraining_type_params,
24};
25use rustc_session::errors::ExprParenthesesNeeded;
26use rustc_span::source_map::Spanned;
27use rustc_span::{ExpnKind, Ident, MacroKind, Span, Symbol, sym};
28use rustc_trait_selection::error_reporting::InferCtxtErrorExt;
29use rustc_trait_selection::error_reporting::traits::DefIdOrName;
30use rustc_trait_selection::infer::InferCtxtExt;
31use rustc_trait_selection::traits;
32use rustc_trait_selection::traits::query::evaluate_obligation::InferCtxtExt as _;
33use tracing::{debug, instrument};
34
35use super::FnCtxt;
36use crate::fn_ctxt::rustc_span::BytePos;
37use crate::method::probe;
38use crate::method::probe::{IsSuggestion, Mode, ProbeScope};
39use crate::{errors, fluent_generated as fluent};
40
41impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
42 pub(crate) fn body_fn_sig(&self) -> Option<ty::FnSig<'tcx>> {
43 self.typeck_results
44 .borrow()
45 .liberated_fn_sigs()
46 .get(self.tcx.local_def_id_to_hir_id(self.body_id))
47 .copied()
48 }
49
50 pub(in super::super) fn suggest_semicolon_at_end(&self, span: Span, err: &mut Diag<'_>) {
51 err.span_suggestion_short(
54 span.shrink_to_hi(),
55 "consider using a semicolon here",
56 ";",
57 Applicability::MaybeIncorrect,
58 );
59 }
60
61 pub(crate) fn suggest_mismatched_types_on_tail(
67 &self,
68 err: &mut Diag<'_>,
69 expr: &'tcx hir::Expr<'tcx>,
70 expected: Ty<'tcx>,
71 found: Ty<'tcx>,
72 blk_id: HirId,
73 ) -> bool {
74 let expr = expr.peel_drop_temps();
75 let mut pointing_at_return_type = false;
76 if let hir::ExprKind::Break(..) = expr.kind {
77 return false;
79 }
80 if let Some((fn_id, fn_decl)) = self.get_fn_decl(blk_id) {
81 pointing_at_return_type =
82 self.suggest_missing_return_type(err, fn_decl, expected, found, fn_id);
83 self.suggest_missing_break_or_return_expr(
84 err, expr, fn_decl, expected, found, blk_id, fn_id,
85 );
86 }
87 pointing_at_return_type
88 }
89
90 pub(crate) fn suggest_fn_call(
98 &self,
99 err: &mut Diag<'_>,
100 expr: &hir::Expr<'_>,
101 found: Ty<'tcx>,
102 can_satisfy: impl FnOnce(Ty<'tcx>) -> bool,
103 ) -> bool {
104 let Some((def_id_or_name, output, inputs)) = self.extract_callable_info(found) else {
105 return false;
106 };
107 if can_satisfy(output) {
108 let (sugg_call, mut applicability) = match inputs.len() {
109 0 => ("".to_string(), Applicability::MachineApplicable),
110 1..=4 => (
111 inputs
112 .iter()
113 .map(|ty| {
114 if ty.is_suggestable(self.tcx, false) {
115 format!("/* {ty} */")
116 } else {
117 "/* value */".to_string()
118 }
119 })
120 .collect::<Vec<_>>()
121 .join(", "),
122 Applicability::HasPlaceholders,
123 ),
124 _ => ("/* ... */".to_string(), Applicability::HasPlaceholders),
125 };
126
127 let msg = match def_id_or_name {
128 DefIdOrName::DefId(def_id) => match self.tcx.def_kind(def_id) {
129 DefKind::Ctor(CtorOf::Struct, _) => "construct this tuple struct".to_string(),
130 DefKind::Ctor(CtorOf::Variant, _) => "construct this tuple variant".to_string(),
131 kind => format!("call this {}", self.tcx.def_kind_descr(kind, def_id)),
132 },
133 DefIdOrName::Name(name) => format!("call this {name}"),
134 };
135
136 let sugg = match expr.kind {
137 hir::ExprKind::Call(..)
138 | hir::ExprKind::Path(..)
139 | hir::ExprKind::Index(..)
140 | hir::ExprKind::Lit(..) => {
141 vec![(expr.span.shrink_to_hi(), format!("({sugg_call})"))]
142 }
143 hir::ExprKind::Closure { .. } => {
144 applicability = Applicability::MaybeIncorrect;
146 vec![
147 (expr.span.shrink_to_lo(), "(".to_string()),
148 (expr.span.shrink_to_hi(), format!(")({sugg_call})")),
149 ]
150 }
151 _ => {
152 vec![
153 (expr.span.shrink_to_lo(), "(".to_string()),
154 (expr.span.shrink_to_hi(), format!(")({sugg_call})")),
155 ]
156 }
157 };
158
159 err.multipart_suggestion_verbose(
160 format!("use parentheses to {msg}"),
161 sugg,
162 applicability,
163 );
164 return true;
165 }
166 false
167 }
168
169 pub(in super::super) fn extract_callable_info(
173 &self,
174 ty: Ty<'tcx>,
175 ) -> Option<(DefIdOrName, Ty<'tcx>, Vec<Ty<'tcx>>)> {
176 self.err_ctxt().extract_callable_info(self.body_id, self.param_env, ty)
177 }
178
179 pub(crate) fn suggest_two_fn_call(
180 &self,
181 err: &mut Diag<'_>,
182 lhs_expr: &'tcx hir::Expr<'tcx>,
183 lhs_ty: Ty<'tcx>,
184 rhs_expr: &'tcx hir::Expr<'tcx>,
185 rhs_ty: Ty<'tcx>,
186 can_satisfy: impl FnOnce(Ty<'tcx>, Ty<'tcx>) -> bool,
187 ) -> bool {
188 if lhs_expr.span.in_derive_expansion() || rhs_expr.span.in_derive_expansion() {
189 return false;
190 }
191 let Some((_, lhs_output_ty, lhs_inputs)) = self.extract_callable_info(lhs_ty) else {
192 return false;
193 };
194 let Some((_, rhs_output_ty, rhs_inputs)) = self.extract_callable_info(rhs_ty) else {
195 return false;
196 };
197
198 if can_satisfy(lhs_output_ty, rhs_output_ty) {
199 let mut sugg = vec![];
200 let mut applicability = Applicability::MachineApplicable;
201
202 for (expr, inputs) in [(lhs_expr, lhs_inputs), (rhs_expr, rhs_inputs)] {
203 let (sugg_call, this_applicability) = match inputs.len() {
204 0 => ("".to_string(), Applicability::MachineApplicable),
205 1..=4 => (
206 inputs
207 .iter()
208 .map(|ty| {
209 if ty.is_suggestable(self.tcx, false) {
210 format!("/* {ty} */")
211 } else {
212 "/* value */".to_string()
213 }
214 })
215 .collect::<Vec<_>>()
216 .join(", "),
217 Applicability::HasPlaceholders,
218 ),
219 _ => ("/* ... */".to_string(), Applicability::HasPlaceholders),
220 };
221
222 applicability = applicability.max(this_applicability);
223
224 match expr.kind {
225 hir::ExprKind::Call(..)
226 | hir::ExprKind::Path(..)
227 | hir::ExprKind::Index(..)
228 | hir::ExprKind::Lit(..) => {
229 sugg.extend([(expr.span.shrink_to_hi(), format!("({sugg_call})"))]);
230 }
231 hir::ExprKind::Closure { .. } => {
232 applicability = Applicability::MaybeIncorrect;
234 sugg.extend([
235 (expr.span.shrink_to_lo(), "(".to_string()),
236 (expr.span.shrink_to_hi(), format!(")({sugg_call})")),
237 ]);
238 }
239 _ => {
240 sugg.extend([
241 (expr.span.shrink_to_lo(), "(".to_string()),
242 (expr.span.shrink_to_hi(), format!(")({sugg_call})")),
243 ]);
244 }
245 }
246 }
247
248 err.multipart_suggestion_verbose("use parentheses to call these", sugg, applicability);
249
250 true
251 } else {
252 false
253 }
254 }
255
256 pub(crate) fn suggest_remove_last_method_call(
257 &self,
258 err: &mut Diag<'_>,
259 expr: &hir::Expr<'tcx>,
260 expected: Ty<'tcx>,
261 ) -> bool {
262 if let hir::ExprKind::MethodCall(hir::PathSegment { ident: method, .. }, recv_expr, &[], _) =
263 expr.kind
264 && let Some(recv_ty) = self.typeck_results.borrow().expr_ty_opt(recv_expr)
265 && self.may_coerce(recv_ty, expected)
266 && let name = method.name.as_str()
267 && (name.starts_with("to_") || name.starts_with("as_") || name == "into")
268 {
269 let span = if let Some(recv_span) = recv_expr.span.find_ancestor_inside(expr.span) {
270 expr.span.with_lo(recv_span.hi())
271 } else {
272 expr.span.with_lo(method.span.lo() - rustc_span::BytePos(1))
273 };
274 err.span_suggestion_verbose(
275 span,
276 "try removing the method call",
277 "",
278 Applicability::MachineApplicable,
279 );
280 return true;
281 }
282 false
283 }
284
285 pub(crate) fn suggest_deref_ref_or_into(
286 &self,
287 err: &mut Diag<'_>,
288 expr: &hir::Expr<'tcx>,
289 expected: Ty<'tcx>,
290 found: Ty<'tcx>,
291 expected_ty_expr: Option<&'tcx hir::Expr<'tcx>>,
292 ) -> bool {
293 let expr = expr.peel_blocks();
294 let methods =
295 self.get_conversion_methods_for_diagnostic(expr.span, expected, found, expr.hir_id);
296
297 if let Some((suggestion, msg, applicability, verbose, annotation)) =
298 self.suggest_deref_or_ref(expr, found, expected)
299 {
300 if verbose {
301 err.multipart_suggestion_verbose(msg, suggestion, applicability);
302 } else {
303 err.multipart_suggestion(msg, suggestion, applicability);
304 }
305 if annotation {
306 let suggest_annotation = match expr.peel_drop_temps().kind {
307 hir::ExprKind::AddrOf(hir::BorrowKind::Ref, mutbl, _) => mutbl.ref_prefix_str(),
308 _ => return true,
309 };
310 let mut tuple_indexes = Vec::new();
311 let mut expr_id = expr.hir_id;
312 for (parent_id, node) in self.tcx.hir_parent_iter(expr.hir_id) {
313 match node {
314 Node::Expr(&Expr { kind: ExprKind::Tup(subs), .. }) => {
315 tuple_indexes.push(
316 subs.iter()
317 .enumerate()
318 .find(|(_, sub_expr)| sub_expr.hir_id == expr_id)
319 .unwrap()
320 .0,
321 );
322 expr_id = parent_id;
323 }
324 Node::LetStmt(local) => {
325 if let Some(mut ty) = local.ty {
326 while let Some(index) = tuple_indexes.pop() {
327 match ty.kind {
328 TyKind::Tup(tys) => ty = &tys[index],
329 _ => return true,
330 }
331 }
332 let annotation_span = ty.span;
333 err.span_suggestion(
334 annotation_span.with_hi(annotation_span.lo()),
335 "alternatively, consider changing the type annotation",
336 suggest_annotation,
337 Applicability::MaybeIncorrect,
338 );
339 }
340 break;
341 }
342 _ => break,
343 }
344 }
345 }
346 return true;
347 }
348
349 if self.suggest_else_fn_with_closure(err, expr, found, expected) {
350 return true;
351 }
352
353 if self.suggest_fn_call(err, expr, found, |output| self.may_coerce(output, expected))
354 && let ty::FnDef(def_id, ..) = *found.kind()
355 && let Some(sp) = self.tcx.hir_span_if_local(def_id)
356 {
357 let name = self.tcx.item_name(def_id);
358 let kind = self.tcx.def_kind(def_id);
359 if let DefKind::Ctor(of, CtorKind::Fn) = kind {
360 err.span_label(
361 sp,
362 format!(
363 "`{name}` defines {} constructor here, which should be called",
364 match of {
365 CtorOf::Struct => "a struct",
366 CtorOf::Variant => "an enum variant",
367 }
368 ),
369 );
370 } else {
371 let descr = self.tcx.def_kind_descr(kind, def_id);
372 err.span_label(sp, format!("{descr} `{name}` defined here"));
373 }
374 return true;
375 }
376
377 if self.suggest_cast(err, expr, found, expected, expected_ty_expr) {
378 return true;
379 }
380
381 if !methods.is_empty() {
382 let mut suggestions = methods
383 .iter()
384 .filter_map(|conversion_method| {
385 let conversion_method_name = conversion_method.name();
386 let receiver_method_ident = expr.method_ident();
387 if let Some(method_ident) = receiver_method_ident
388 && method_ident.name == conversion_method_name
389 {
390 return None; }
392
393 let method_call_list = [sym::to_vec, sym::to_string];
394 let mut sugg = if let ExprKind::MethodCall(receiver_method, ..) = expr.kind
395 && receiver_method.ident.name == sym::clone
396 && method_call_list.contains(&conversion_method_name)
397 {
402 vec![(receiver_method.ident.span, conversion_method_name.to_string())]
403 } else if self.precedence(expr) < ExprPrecedence::Unambiguous {
404 vec![
405 (expr.span.shrink_to_lo(), "(".to_string()),
406 (expr.span.shrink_to_hi(), format!(").{}()", conversion_method_name)),
407 ]
408 } else {
409 vec![(expr.span.shrink_to_hi(), format!(".{}()", conversion_method_name))]
410 };
411 let struct_pat_shorthand_field =
412 self.tcx.hir_maybe_get_struct_pattern_shorthand_field(expr);
413 if let Some(name) = struct_pat_shorthand_field {
414 sugg.insert(0, (expr.span.shrink_to_lo(), format!("{name}: ")));
415 }
416 Some(sugg)
417 })
418 .peekable();
419 if suggestions.peek().is_some() {
420 err.multipart_suggestions(
421 "try using a conversion method",
422 suggestions,
423 Applicability::MaybeIncorrect,
424 );
425 return true;
426 }
427 }
428
429 if let Some((found_ty_inner, expected_ty_inner, error_tys)) =
430 self.deconstruct_option_or_result(found, expected)
431 && let ty::Ref(_, peeled, hir::Mutability::Not) = *expected_ty_inner.kind()
432 {
433 let inner_expr = expr.peel_borrows();
435 if !inner_expr.span.eq_ctxt(expr.span) {
436 return false;
437 }
438 let borrow_removal_span = if inner_expr.hir_id == expr.hir_id {
439 None
440 } else {
441 Some(expr.span.shrink_to_lo().until(inner_expr.span))
442 };
443 let error_tys_equate_as_ref = error_tys.is_none_or(|(found, expected)| {
446 self.can_eq(
447 self.param_env,
448 Ty::new_imm_ref(self.tcx, self.tcx.lifetimes.re_erased, found),
449 expected,
450 )
451 });
452
453 let prefix_wrap = |sugg: &str| {
454 if let Some(name) = self.tcx.hir_maybe_get_struct_pattern_shorthand_field(expr) {
455 format!(": {}{}", name, sugg)
456 } else {
457 sugg.to_string()
458 }
459 };
460
461 if self.can_eq(self.param_env, found_ty_inner, peeled) && error_tys_equate_as_ref {
464 let sugg = prefix_wrap(".as_ref()");
465 err.subdiagnostic(errors::SuggestConvertViaMethod {
466 span: expr.span.shrink_to_hi(),
467 sugg,
468 expected,
469 found,
470 borrow_removal_span,
471 });
472 return true;
473 } else if let ty::Ref(_, peeled_found_ty, _) = found_ty_inner.kind()
474 && let ty::Adt(adt, _) = peeled_found_ty.peel_refs().kind()
475 && self.tcx.is_lang_item(adt.did(), LangItem::String)
476 && peeled.is_str()
477 && error_tys.is_none_or(|(found, expected)| {
479 self.can_eq(self.param_env, found, expected)
480 })
481 {
482 let sugg = prefix_wrap(".map(|x| x.as_str())");
483 err.span_suggestion_verbose(
484 expr.span.shrink_to_hi(),
485 fluent::hir_typeck_convert_to_str,
486 sugg,
487 Applicability::MachineApplicable,
488 );
489 return true;
490 } else {
491 if !error_tys_equate_as_ref {
492 return false;
493 }
494 let mut steps = self.autoderef(expr.span, found_ty_inner).silence_errors();
495 if let Some((deref_ty, _)) = steps.nth(1)
496 && self.can_eq(self.param_env, deref_ty, peeled)
497 {
498 let sugg = prefix_wrap(".as_deref()");
499 err.subdiagnostic(errors::SuggestConvertViaMethod {
500 span: expr.span.shrink_to_hi(),
501 sugg,
502 expected,
503 found,
504 borrow_removal_span,
505 });
506 return true;
507 }
508 for (deref_ty, n_step) in steps {
509 if self.can_eq(self.param_env, deref_ty, peeled) {
510 let explicit_deref = "*".repeat(n_step);
511 let sugg = prefix_wrap(&format!(".map(|v| &{explicit_deref}v)"));
512 err.subdiagnostic(errors::SuggestConvertViaMethod {
513 span: expr.span.shrink_to_hi(),
514 sugg,
515 expected,
516 found,
517 borrow_removal_span,
518 });
519 return true;
520 }
521 }
522 }
523 }
524
525 false
526 }
527
528 fn deconstruct_option_or_result(
532 &self,
533 found_ty: Ty<'tcx>,
534 expected_ty: Ty<'tcx>,
535 ) -> Option<(Ty<'tcx>, Ty<'tcx>, Option<(Ty<'tcx>, Ty<'tcx>)>)> {
536 let ty::Adt(found_adt, found_args) = found_ty.peel_refs().kind() else {
537 return None;
538 };
539 let ty::Adt(expected_adt, expected_args) = expected_ty.kind() else {
540 return None;
541 };
542 if self.tcx.is_diagnostic_item(sym::Option, found_adt.did())
543 && self.tcx.is_diagnostic_item(sym::Option, expected_adt.did())
544 {
545 Some((found_args.type_at(0), expected_args.type_at(0), None))
546 } else if self.tcx.is_diagnostic_item(sym::Result, found_adt.did())
547 && self.tcx.is_diagnostic_item(sym::Result, expected_adt.did())
548 {
549 Some((
550 found_args.type_at(0),
551 expected_args.type_at(0),
552 Some((found_args.type_at(1), expected_args.type_at(1))),
553 ))
554 } else {
555 None
556 }
557 }
558
559 pub(in super::super) fn suggest_boxing_when_appropriate(
562 &self,
563 err: &mut Diag<'_>,
564 span: Span,
565 hir_id: HirId,
566 expected: Ty<'tcx>,
567 found: Ty<'tcx>,
568 ) -> bool {
569 if self.tcx.hir_is_inside_const_context(hir_id) || !expected.is_box() || found.is_box() {
571 return false;
572 }
573 if self.may_coerce(Ty::new_box(self.tcx, found), expected) {
574 let suggest_boxing = match found.kind() {
575 ty::Tuple(tuple) if tuple.is_empty() => {
576 errors::SuggestBoxing::Unit { start: span.shrink_to_lo(), end: span }
577 }
578 ty::Coroutine(def_id, ..)
579 if matches!(
580 self.tcx.coroutine_kind(def_id),
581 Some(CoroutineKind::Desugared(
582 CoroutineDesugaring::Async,
583 CoroutineSource::Closure
584 ))
585 ) =>
586 {
587 errors::SuggestBoxing::AsyncBody
588 }
589 _ if let Node::ExprField(expr_field) = self.tcx.parent_hir_node(hir_id)
590 && expr_field.is_shorthand =>
591 {
592 errors::SuggestBoxing::ExprFieldShorthand {
593 start: span.shrink_to_lo(),
594 end: span.shrink_to_hi(),
595 ident: expr_field.ident,
596 }
597 }
598 _ => errors::SuggestBoxing::Other {
599 start: span.shrink_to_lo(),
600 end: span.shrink_to_hi(),
601 },
602 };
603 err.subdiagnostic(suggest_boxing);
604
605 true
606 } else {
607 false
608 }
609 }
610
611 pub(in super::super) fn suggest_no_capture_closure(
614 &self,
615 err: &mut Diag<'_>,
616 expected: Ty<'tcx>,
617 found: Ty<'tcx>,
618 ) -> bool {
619 if let (ty::FnPtr(..), ty::Closure(def_id, _)) = (expected.kind(), found.kind())
620 && let Some(upvars) = self.tcx.upvars_mentioned(*def_id)
621 {
622 let spans_and_labels = upvars
625 .iter()
626 .take(4)
627 .map(|(var_hir_id, upvar)| {
628 let var_name = self.tcx.hir_name(*var_hir_id).to_string();
629 let msg = format!("`{var_name}` captured here");
630 (upvar.span, msg)
631 })
632 .collect::<Vec<_>>();
633
634 let mut multi_span: MultiSpan =
635 spans_and_labels.iter().map(|(sp, _)| *sp).collect::<Vec<_>>().into();
636 for (sp, label) in spans_and_labels {
637 multi_span.push_span_label(sp, label);
638 }
639 err.span_note(
640 multi_span,
641 "closures can only be coerced to `fn` types if they do not capture any variables",
642 );
643 return true;
644 }
645 false
646 }
647
648 #[instrument(skip(self, err))]
650 pub(in super::super) fn suggest_calling_boxed_future_when_appropriate(
651 &self,
652 err: &mut Diag<'_>,
653 expr: &hir::Expr<'_>,
654 expected: Ty<'tcx>,
655 found: Ty<'tcx>,
656 ) -> bool {
657 if self.tcx.hir_is_inside_const_context(expr.hir_id) {
660 return false;
662 }
663 let pin_did = self.tcx.lang_items().pin_type();
664 if pin_did.is_none() || self.tcx.lang_items().owned_box().is_none() {
666 return false;
667 }
668 let box_found = Ty::new_box(self.tcx, found);
669 let Some(pin_box_found) = Ty::new_lang_item(self.tcx, box_found, LangItem::Pin) else {
670 return false;
671 };
672 let Some(pin_found) = Ty::new_lang_item(self.tcx, found, LangItem::Pin) else {
673 return false;
674 };
675 match expected.kind() {
676 ty::Adt(def, _) if Some(def.did()) == pin_did => {
677 if self.may_coerce(pin_box_found, expected) {
678 debug!("can coerce {:?} to {:?}, suggesting Box::pin", pin_box_found, expected);
679 match found.kind() {
680 ty::Adt(def, _) if def.is_box() => {
681 err.help("use `Box::pin`");
682 }
683 _ => {
684 let prefix = if let Some(name) =
685 self.tcx.hir_maybe_get_struct_pattern_shorthand_field(expr)
686 {
687 format!("{}: ", name)
688 } else {
689 String::new()
690 };
691 let suggestion = vec![
692 (expr.span.shrink_to_lo(), format!("{prefix}Box::pin(")),
693 (expr.span.shrink_to_hi(), ")".to_string()),
694 ];
695 err.multipart_suggestion(
696 "you need to pin and box this expression",
697 suggestion,
698 Applicability::MaybeIncorrect,
699 );
700 }
701 }
702 true
703 } else if self.may_coerce(pin_found, expected) {
704 match found.kind() {
705 ty::Adt(def, _) if def.is_box() => {
706 err.help("use `Box::pin`");
707 true
708 }
709 _ => false,
710 }
711 } else {
712 false
713 }
714 }
715 ty::Adt(def, _) if def.is_box() && self.may_coerce(box_found, expected) => {
716 let Node::Expr(Expr { kind: ExprKind::Call(fn_name, _), .. }) =
720 self.tcx.parent_hir_node(expr.hir_id)
721 else {
722 return false;
723 };
724 match fn_name.kind {
725 ExprKind::Path(QPath::TypeRelative(
726 hir::Ty {
727 kind: TyKind::Path(QPath::Resolved(_, Path { res: recv_ty, .. })),
728 ..
729 },
730 method,
731 )) if recv_ty.opt_def_id() == pin_did && method.ident.name == sym::new => {
732 err.span_suggestion(
733 fn_name.span,
734 "use `Box::pin` to pin and box this expression",
735 "Box::pin",
736 Applicability::MachineApplicable,
737 );
738 true
739 }
740 _ => false,
741 }
742 }
743 _ => false,
744 }
745 }
746
747 pub(crate) fn suggest_missing_semicolon(
763 &self,
764 err: &mut Diag<'_>,
765 expression: &'tcx hir::Expr<'tcx>,
766 expected: Ty<'tcx>,
767 needs_block: bool,
768 parent_is_closure: bool,
769 ) {
770 if !expected.is_unit() {
771 return;
772 }
773 match expression.kind {
776 ExprKind::Call(..)
777 | ExprKind::MethodCall(..)
778 | ExprKind::Loop(..)
779 | ExprKind::If(..)
780 | ExprKind::Match(..)
781 | ExprKind::Block(..)
782 if expression.can_have_side_effects()
783 && !expression.span.in_external_macro(self.tcx.sess.source_map()) =>
787 {
788 if needs_block {
789 err.multipart_suggestion(
790 "consider using a semicolon here",
791 vec![
792 (expression.span.shrink_to_lo(), "{ ".to_owned()),
793 (expression.span.shrink_to_hi(), "; }".to_owned()),
794 ],
795 Applicability::MachineApplicable,
796 );
797 } else if let hir::Node::Block(block) = self.tcx.parent_hir_node(expression.hir_id)
798 && let hir::Node::Expr(expr) = self.tcx.parent_hir_node(block.hir_id)
799 && let hir::Node::Expr(if_expr) = self.tcx.parent_hir_node(expr.hir_id)
800 && let hir::ExprKind::If(_cond, _then, Some(_else)) = if_expr.kind
801 && let hir::Node::Stmt(stmt) = self.tcx.parent_hir_node(if_expr.hir_id)
802 && let hir::StmtKind::Expr(_) = stmt.kind
803 && self.is_next_stmt_expr_continuation(stmt.hir_id)
804 {
805 err.multipart_suggestion(
806 "parentheses are required to parse this as an expression",
807 vec![
808 (stmt.span.shrink_to_lo(), "(".to_string()),
809 (stmt.span.shrink_to_hi(), ")".to_string()),
810 ],
811 Applicability::MachineApplicable,
812 );
813 } else {
814 err.span_suggestion(
815 expression.span.shrink_to_hi(),
816 "consider using a semicolon here",
817 ";",
818 Applicability::MachineApplicable,
819 );
820 }
821 }
822 ExprKind::Path(..) | ExprKind::Lit(_)
823 if parent_is_closure
824 && !expression.span.in_external_macro(self.tcx.sess.source_map()) =>
825 {
826 err.span_suggestion_verbose(
827 expression.span.shrink_to_lo(),
828 "consider ignoring the value",
829 "_ = ",
830 Applicability::MachineApplicable,
831 );
832 }
833 _ => {
834 if let hir::Node::Block(block) = self.tcx.parent_hir_node(expression.hir_id)
835 && let hir::Node::Expr(expr) = self.tcx.parent_hir_node(block.hir_id)
836 && let hir::Node::Expr(if_expr) = self.tcx.parent_hir_node(expr.hir_id)
837 && let hir::ExprKind::If(_cond, _then, Some(_else)) = if_expr.kind
838 && let hir::Node::Stmt(stmt) = self.tcx.parent_hir_node(if_expr.hir_id)
839 && let hir::StmtKind::Expr(_) = stmt.kind
840 && self.is_next_stmt_expr_continuation(stmt.hir_id)
841 {
842 err.multipart_suggestion(
850 "parentheses are required to parse this as an expression",
851 vec![
852 (stmt.span.shrink_to_lo(), "(".to_string()),
853 (stmt.span.shrink_to_hi(), ")".to_string()),
854 ],
855 Applicability::MachineApplicable,
856 );
857 }
858 }
859 }
860 }
861
862 pub(crate) fn is_next_stmt_expr_continuation(&self, hir_id: HirId) -> bool {
863 if let hir::Node::Block(b) = self.tcx.parent_hir_node(hir_id)
864 && let mut stmts = b.stmts.iter().skip_while(|s| s.hir_id != hir_id)
865 && let Some(_) = stmts.next() && let Some(next) = match (stmts.next(), b.expr) { (Some(next), _) => match next.kind {
868 hir::StmtKind::Expr(next) | hir::StmtKind::Semi(next) => Some(next),
869 _ => None,
870 },
871 (None, Some(next)) => Some(next),
872 _ => None,
873 }
874 && let hir::ExprKind::AddrOf(..) | hir::ExprKind::Unary(..) | hir::ExprKind::Err(_) = next.kind
877 {
879 true
880 } else {
881 false
882 }
883 }
884
885 #[instrument(level = "trace", skip(self, err))]
898 pub(in super::super) fn suggest_missing_return_type(
899 &self,
900 err: &mut Diag<'_>,
901 fn_decl: &hir::FnDecl<'tcx>,
902 expected: Ty<'tcx>,
903 found: Ty<'tcx>,
904 fn_id: LocalDefId,
905 ) -> bool {
906 if let Some(hir::CoroutineKind::Desugared(_, hir::CoroutineSource::Block)) =
908 self.tcx.coroutine_kind(fn_id)
909 {
910 return false;
911 }
912
913 let found =
914 self.resolve_numeric_literals_with_default(self.resolve_vars_if_possible(found));
915 match &fn_decl.output {
918 &hir::FnRetTy::DefaultReturn(_) if self.tcx.is_closure_like(fn_id.to_def_id()) => {}
920 &hir::FnRetTy::DefaultReturn(span) if expected.is_unit() => {
921 if !self.can_add_return_type(fn_id) {
922 err.subdiagnostic(errors::ExpectedReturnTypeLabel::Unit { span });
923 } else if let Some(found) = found.make_suggestable(self.tcx, false, None) {
924 err.subdiagnostic(errors::AddReturnTypeSuggestion::Add {
925 span,
926 found: found.to_string(),
927 });
928 } else if let Some(sugg) = suggest_impl_trait(self, self.param_env, found) {
929 err.subdiagnostic(errors::AddReturnTypeSuggestion::Add { span, found: sugg });
930 } else {
931 err.subdiagnostic(errors::AddReturnTypeSuggestion::MissingHere { span });
933 }
934
935 return true;
936 }
937 hir::FnRetTy::Return(hir_ty) => {
938 if let hir::TyKind::OpaqueDef(op_ty, ..) = hir_ty.kind
939 && let [hir::GenericBound::Trait(trait_ref)] = op_ty.bounds
941 && let Some(hir::PathSegment { args: Some(generic_args), .. }) =
942 trait_ref.trait_ref.path.segments.last()
943 && let [constraint] = generic_args.constraints
944 && let Some(ty) = constraint.ty()
945 {
946 debug!(?found);
949 if found.is_suggestable(self.tcx, false) {
950 if ty.span.is_empty() {
951 err.subdiagnostic(errors::AddReturnTypeSuggestion::Add {
952 span: ty.span,
953 found: found.to_string(),
954 });
955 return true;
956 } else {
957 err.subdiagnostic(errors::ExpectedReturnTypeLabel::Other {
958 span: ty.span,
959 expected,
960 });
961 }
962 }
963 } else {
964 debug!(?hir_ty, "return type");
967 let ty = self.lowerer().lower_ty(hir_ty);
968 debug!(?ty, "return type (lowered)");
969 debug!(?expected, "expected type");
970 let bound_vars =
971 self.tcx.late_bound_vars(self.tcx.local_def_id_to_hir_id(fn_id));
972 let ty = Binder::bind_with_vars(ty, bound_vars);
973 let ty = self.normalize(hir_ty.span, ty);
974 let ty = self.tcx.instantiate_bound_regions_with_erased(ty);
975 if self.may_coerce(expected, ty) {
976 err.subdiagnostic(errors::ExpectedReturnTypeLabel::Other {
977 span: hir_ty.span,
978 expected,
979 });
980 self.try_suggest_return_impl_trait(err, expected, found, fn_id);
981 self.try_note_caller_chooses_ty_for_ty_param(err, expected, found);
982 return true;
983 }
984 }
985 }
986 _ => {}
987 }
988 false
989 }
990
991 fn can_add_return_type(&self, fn_id: LocalDefId) -> bool {
994 match self.tcx.hir_node_by_def_id(fn_id) {
995 Node::Item(item) => {
996 let (ident, _, _, _) = item.expect_fn();
997 ident.name != sym::main
1001 }
1002 Node::ImplItem(item) => {
1003 let Node::Item(&hir::Item {
1005 kind: hir::ItemKind::Impl(hir::Impl { of_trait, .. }),
1006 ..
1007 }) = self.tcx.parent_hir_node(item.hir_id())
1008 else {
1009 unreachable!();
1010 };
1011
1012 of_trait.is_none()
1013 }
1014 _ => true,
1015 }
1016 }
1017
1018 fn try_note_caller_chooses_ty_for_ty_param(
1019 &self,
1020 diag: &mut Diag<'_>,
1021 expected: Ty<'tcx>,
1022 found: Ty<'tcx>,
1023 ) {
1024 let ty::Param(expected_ty_as_param) = expected.kind() else {
1031 return;
1032 };
1033
1034 if found.contains(expected) {
1035 return;
1036 }
1037
1038 diag.subdiagnostic(errors::NoteCallerChoosesTyForTyParam {
1039 ty_param_name: expected_ty_as_param.name,
1040 found_ty: found,
1041 });
1042 }
1043
1044 fn try_suggest_return_impl_trait(
1053 &self,
1054 err: &mut Diag<'_>,
1055 expected: Ty<'tcx>,
1056 found: Ty<'tcx>,
1057 fn_id: LocalDefId,
1058 ) {
1059 debug!("try_suggest_return_impl_trait, expected = {:?}, found = {:?}", expected, found);
1068
1069 let ty::Param(expected_ty_as_param) = expected.kind() else { return };
1070
1071 let fn_node = self.tcx.hir_node_by_def_id(fn_id);
1072
1073 let hir::Node::Item(hir::Item {
1074 kind:
1075 hir::ItemKind::Fn {
1076 sig:
1077 hir::FnSig {
1078 decl: hir::FnDecl { inputs: fn_parameters, output: fn_return, .. },
1079 ..
1080 },
1081 generics: hir::Generics { params, predicates, .. },
1082 ..
1083 },
1084 ..
1085 }) = fn_node
1086 else {
1087 return;
1088 };
1089
1090 if params.get(expected_ty_as_param.index as usize).is_none() {
1091 return;
1092 };
1093
1094 let where_predicates = predicates
1096 .iter()
1097 .filter_map(|p| match p.kind {
1098 WherePredicateKind::BoundPredicate(hir::WhereBoundPredicate {
1099 bounds,
1100 bounded_ty,
1101 ..
1102 }) => {
1103 let ty = self.lowerer().lower_ty(bounded_ty);
1105 Some((ty, bounds))
1106 }
1107 _ => None,
1108 })
1109 .map(|(ty, bounds)| match ty.kind() {
1110 ty::Param(param_ty) if param_ty == expected_ty_as_param => Ok(Some(bounds)),
1111 _ => match ty.contains(expected) {
1113 true => Err(()),
1114 false => Ok(None),
1115 },
1116 })
1117 .collect::<Result<Vec<_>, _>>();
1118
1119 let Ok(where_predicates) = where_predicates else { return };
1120
1121 let predicates_from_where =
1123 where_predicates.iter().flatten().flat_map(|bounds| bounds.iter());
1124
1125 let all_matching_bounds_strs = predicates_from_where
1127 .filter_map(|bound| match bound {
1128 GenericBound::Trait(_) => {
1129 self.tcx.sess.source_map().span_to_snippet(bound.span()).ok()
1130 }
1131 _ => None,
1132 })
1133 .collect::<Vec<String>>();
1134
1135 if all_matching_bounds_strs.is_empty() {
1136 return;
1137 }
1138
1139 let all_bounds_str = all_matching_bounds_strs.join(" + ");
1140
1141 let ty_param_used_in_fn_params = fn_parameters.iter().any(|param| {
1142 let ty = self.lowerer().lower_ty( param);
1143 matches!(ty.kind(), ty::Param(fn_param_ty_param) if expected_ty_as_param == fn_param_ty_param)
1144 });
1145
1146 if ty_param_used_in_fn_params {
1147 return;
1148 }
1149
1150 err.span_suggestion(
1151 fn_return.span(),
1152 "consider using an impl return type",
1153 format!("impl {all_bounds_str}"),
1154 Applicability::MaybeIncorrect,
1155 );
1156 }
1157
1158 pub(in super::super) fn suggest_missing_break_or_return_expr(
1159 &self,
1160 err: &mut Diag<'_>,
1161 expr: &'tcx hir::Expr<'tcx>,
1162 fn_decl: &hir::FnDecl<'tcx>,
1163 expected: Ty<'tcx>,
1164 found: Ty<'tcx>,
1165 id: HirId,
1166 fn_id: LocalDefId,
1167 ) {
1168 if !expected.is_unit() {
1169 return;
1170 }
1171 let found = self.resolve_vars_if_possible(found);
1172
1173 let in_loop = self.is_loop(id)
1174 || self
1175 .tcx
1176 .hir_parent_iter(id)
1177 .take_while(|(_, node)| {
1178 node.body_id().is_none()
1180 })
1181 .any(|(parent_id, _)| self.is_loop(parent_id));
1182
1183 let in_local_statement = self.is_local_statement(id)
1184 || self
1185 .tcx
1186 .hir_parent_iter(id)
1187 .any(|(parent_id, _)| self.is_local_statement(parent_id));
1188
1189 if in_loop && in_local_statement {
1190 err.multipart_suggestion(
1191 "you might have meant to break the loop with this value",
1192 vec![
1193 (expr.span.shrink_to_lo(), "break ".to_string()),
1194 (expr.span.shrink_to_hi(), ";".to_string()),
1195 ],
1196 Applicability::MaybeIncorrect,
1197 );
1198 return;
1199 }
1200
1201 let scope = self.tcx.hir_parent_iter(id).find(|(_, node)| {
1202 matches!(
1203 node,
1204 Node::Expr(Expr { kind: ExprKind::Closure(..), .. })
1205 | Node::Item(_)
1206 | Node::TraitItem(_)
1207 | Node::ImplItem(_)
1208 )
1209 });
1210 let in_closure =
1211 matches!(scope, Some((_, Node::Expr(Expr { kind: ExprKind::Closure(..), .. }))));
1212
1213 let can_return = match fn_decl.output {
1214 hir::FnRetTy::Return(ty) => {
1215 let ty = self.lowerer().lower_ty(ty);
1216 let bound_vars = self.tcx.late_bound_vars(self.tcx.local_def_id_to_hir_id(fn_id));
1217 let ty = self
1218 .tcx
1219 .instantiate_bound_regions_with_erased(Binder::bind_with_vars(ty, bound_vars));
1220 let ty = match self.tcx.asyncness(fn_id) {
1221 ty::Asyncness::Yes => {
1222 self.err_ctxt().get_impl_future_output_ty(ty).unwrap_or_else(|| {
1223 span_bug!(
1224 fn_decl.output.span(),
1225 "failed to get output type of async function"
1226 )
1227 })
1228 }
1229 ty::Asyncness::No => ty,
1230 };
1231 let ty = self.normalize(expr.span, ty);
1232 self.may_coerce(found, ty)
1233 }
1234 hir::FnRetTy::DefaultReturn(_) if in_closure => {
1235 self.ret_coercion.as_ref().is_some_and(|ret| {
1236 let ret_ty = ret.borrow().expected_ty();
1237 self.may_coerce(found, ret_ty)
1238 })
1239 }
1240 _ => false,
1241 };
1242 if can_return
1243 && let Some(span) = expr.span.find_ancestor_inside(
1244 self.tcx.hir_span_with_body(self.tcx.local_def_id_to_hir_id(fn_id)),
1245 )
1246 {
1247 fn is_in_arm<'tcx>(expr: &'tcx hir::Expr<'tcx>, tcx: TyCtxt<'tcx>) -> bool {
1258 for (_, node) in tcx.hir_parent_iter(expr.hir_id) {
1259 match node {
1260 hir::Node::Block(block) => {
1261 if let Some(ret) = block.expr
1262 && ret.hir_id == expr.hir_id
1263 {
1264 continue;
1265 }
1266 }
1267 hir::Node::Arm(arm) => {
1268 if let hir::ExprKind::Block(block, _) = arm.body.kind
1269 && let Some(ret) = block.expr
1270 && ret.hir_id == expr.hir_id
1271 {
1272 return true;
1273 }
1274 }
1275 hir::Node::Expr(e) if let hir::ExprKind::Block(block, _) = e.kind => {
1276 if let Some(ret) = block.expr
1277 && ret.hir_id == expr.hir_id
1278 {
1279 continue;
1280 }
1281 }
1282 _ => {
1283 return false;
1284 }
1285 }
1286 }
1287
1288 false
1289 }
1290 let mut suggs = vec![(span.shrink_to_lo(), "return ".to_string())];
1291 if !is_in_arm(expr, self.tcx) {
1292 suggs.push((span.shrink_to_hi(), ";".to_string()));
1293 }
1294 err.multipart_suggestion_verbose(
1295 "you might have meant to return this value",
1296 suggs,
1297 Applicability::MaybeIncorrect,
1298 );
1299 }
1300 }
1301
1302 pub(in super::super) fn suggest_missing_parentheses(
1303 &self,
1304 err: &mut Diag<'_>,
1305 expr: &hir::Expr<'_>,
1306 ) -> bool {
1307 let sp = self.tcx.sess.source_map().start_point(expr.span).with_parent(None);
1308 if let Some(sp) = self.tcx.sess.psess.ambiguous_block_expr_parse.borrow().get(&sp) {
1309 err.subdiagnostic(ExprParenthesesNeeded::surrounding(*sp));
1311 true
1312 } else {
1313 false
1314 }
1315 }
1316
1317 pub(crate) fn suggest_block_to_brackets_peeling_refs(
1321 &self,
1322 diag: &mut Diag<'_>,
1323 mut expr: &hir::Expr<'_>,
1324 mut expr_ty: Ty<'tcx>,
1325 mut expected_ty: Ty<'tcx>,
1326 ) -> bool {
1327 loop {
1328 match (&expr.kind, expr_ty.kind(), expected_ty.kind()) {
1329 (
1330 hir::ExprKind::AddrOf(_, _, inner_expr),
1331 ty::Ref(_, inner_expr_ty, _),
1332 ty::Ref(_, inner_expected_ty, _),
1333 ) => {
1334 expr = *inner_expr;
1335 expr_ty = *inner_expr_ty;
1336 expected_ty = *inner_expected_ty;
1337 }
1338 (hir::ExprKind::Block(blk, _), _, _) => {
1339 self.suggest_block_to_brackets(diag, blk, expr_ty, expected_ty);
1340 break true;
1341 }
1342 _ => break false,
1343 }
1344 }
1345 }
1346
1347 pub(crate) fn suggest_clone_for_ref(
1348 &self,
1349 diag: &mut Diag<'_>,
1350 expr: &hir::Expr<'_>,
1351 expr_ty: Ty<'tcx>,
1352 expected_ty: Ty<'tcx>,
1353 ) -> bool {
1354 if let ty::Ref(_, inner_ty, hir::Mutability::Not) = expr_ty.kind()
1355 && let Some(clone_trait_def) = self.tcx.lang_items().clone_trait()
1356 && expected_ty == *inner_ty
1357 && self
1358 .infcx
1359 .type_implements_trait(
1360 clone_trait_def,
1361 [self.tcx.erase_and_anonymize_regions(expected_ty)],
1362 self.param_env,
1363 )
1364 .must_apply_modulo_regions()
1365 {
1366 let suggestion = match self.tcx.hir_maybe_get_struct_pattern_shorthand_field(expr) {
1367 Some(ident) => format!(": {ident}.clone()"),
1368 None => ".clone()".to_string(),
1369 };
1370
1371 let span = expr.span.find_ancestor_not_from_macro().unwrap_or(expr.span).shrink_to_hi();
1372
1373 diag.span_suggestion_verbose(
1374 span,
1375 "consider using clone here",
1376 suggestion,
1377 Applicability::MachineApplicable,
1378 );
1379 return true;
1380 }
1381 false
1382 }
1383
1384 pub(crate) fn suggest_copied_cloned_or_as_ref(
1385 &self,
1386 diag: &mut Diag<'_>,
1387 expr: &hir::Expr<'_>,
1388 expr_ty: Ty<'tcx>,
1389 expected_ty: Ty<'tcx>,
1390 ) -> bool {
1391 let ty::Adt(adt_def, args) = expr_ty.kind() else {
1392 return false;
1393 };
1394 let ty::Adt(expected_adt_def, expected_args) = expected_ty.kind() else {
1395 return false;
1396 };
1397 if adt_def != expected_adt_def {
1398 return false;
1399 }
1400
1401 if Some(adt_def.did()) == self.tcx.get_diagnostic_item(sym::Result)
1402 && self.can_eq(self.param_env, args.type_at(1), expected_args.type_at(1))
1403 || Some(adt_def.did()) == self.tcx.get_diagnostic_item(sym::Option)
1404 {
1405 let expr_inner_ty = args.type_at(0);
1406 let expected_inner_ty = expected_args.type_at(0);
1407 if let &ty::Ref(_, ty, _mutability) = expr_inner_ty.kind()
1408 && self.can_eq(self.param_env, ty, expected_inner_ty)
1409 {
1410 let def_path = self.tcx.def_path_str(adt_def.did());
1411 let span = expr.span.shrink_to_hi();
1412 let subdiag = if self.type_is_copy_modulo_regions(self.param_env, ty) {
1413 errors::OptionResultRefMismatch::Copied { span, def_path }
1414 } else if self.type_is_clone_modulo_regions(self.param_env, ty) {
1415 errors::OptionResultRefMismatch::Cloned { span, def_path }
1416 } else {
1417 return false;
1418 };
1419 diag.subdiagnostic(subdiag);
1420 return true;
1421 }
1422 }
1423
1424 false
1425 }
1426
1427 pub(crate) fn suggest_into(
1428 &self,
1429 diag: &mut Diag<'_>,
1430 expr: &hir::Expr<'_>,
1431 expr_ty: Ty<'tcx>,
1432 expected_ty: Ty<'tcx>,
1433 ) -> bool {
1434 let expr = expr.peel_blocks();
1435
1436 if expr_ty.is_scalar() && expected_ty.is_scalar() {
1438 return false;
1439 }
1440
1441 if matches!(expr.kind, hir::ExprKind::Block(..)) {
1443 return false;
1444 }
1445
1446 if self.err_ctxt().should_suggest_as_ref(expected_ty, expr_ty).is_some() {
1449 return false;
1450 }
1451
1452 if let Some(into_def_id) = self.tcx.get_diagnostic_item(sym::Into)
1453 && self.predicate_must_hold_modulo_regions(&traits::Obligation::new(
1454 self.tcx,
1455 self.misc(expr.span),
1456 self.param_env,
1457 ty::TraitRef::new(self.tcx, into_def_id, [expr_ty, expected_ty]),
1458 ))
1459 && !expr
1460 .span
1461 .macro_backtrace()
1462 .any(|x| matches!(x.kind, ExpnKind::Macro(MacroKind::Attr | MacroKind::Derive, ..)))
1463 {
1464 let span = expr
1465 .span
1466 .find_ancestor_not_from_extern_macro(self.tcx.sess.source_map())
1467 .unwrap_or(expr.span);
1468
1469 let mut sugg = if self.precedence(expr) >= ExprPrecedence::Unambiguous {
1470 vec![(span.shrink_to_hi(), ".into()".to_owned())]
1471 } else {
1472 vec![
1473 (span.shrink_to_lo(), "(".to_owned()),
1474 (span.shrink_to_hi(), ").into()".to_owned()),
1475 ]
1476 };
1477 if let Some(name) = self.tcx.hir_maybe_get_struct_pattern_shorthand_field(expr) {
1478 sugg.insert(0, (expr.span.shrink_to_lo(), format!("{}: ", name)));
1479 }
1480 diag.multipart_suggestion(
1481 format!("call `Into::into` on this expression to convert `{expr_ty}` into `{expected_ty}`"),
1482 sugg,
1483 Applicability::MaybeIncorrect
1484 );
1485 return true;
1486 }
1487
1488 false
1489 }
1490
1491 pub(crate) fn suggest_option_to_bool(
1493 &self,
1494 diag: &mut Diag<'_>,
1495 expr: &hir::Expr<'_>,
1496 expr_ty: Ty<'tcx>,
1497 expected_ty: Ty<'tcx>,
1498 ) -> bool {
1499 if !expected_ty.is_bool() {
1500 return false;
1501 }
1502
1503 let ty::Adt(def, _) = expr_ty.peel_refs().kind() else {
1504 return false;
1505 };
1506 if !self.tcx.is_diagnostic_item(sym::Option, def.did()) {
1507 return false;
1508 }
1509
1510 let cond_parent = self.tcx.hir_parent_iter(expr.hir_id).find(|(_, node)| {
1511 !matches!(node, hir::Node::Expr(hir::Expr { kind: hir::ExprKind::Binary(op, _, _), .. }) if op.node == hir::BinOpKind::And)
1512 });
1513 if let Some((_, hir::Node::LetStmt(local))) = cond_parent
1519 && let hir::PatKind::Expr(PatExpr { kind: PatExprKind::Path(qpath), .. })
1520 | hir::PatKind::TupleStruct(qpath, _, _) = &local.pat.kind
1521 && let hir::QPath::Resolved(None, path) = qpath
1522 && let Some(did) = path
1523 .res
1524 .opt_def_id()
1525 .and_then(|did| self.tcx.opt_parent(did))
1526 .and_then(|did| self.tcx.opt_parent(did))
1527 && self.tcx.is_diagnostic_item(sym::Option, did)
1528 {
1529 return false;
1530 }
1531
1532 let suggestion = match self.tcx.hir_maybe_get_struct_pattern_shorthand_field(expr) {
1533 Some(ident) => format!(": {ident}.is_some()"),
1534 None => ".is_some()".to_string(),
1535 };
1536
1537 diag.span_suggestion_verbose(
1538 expr.span.shrink_to_hi(),
1539 "use `Option::is_some` to test if the `Option` has a value",
1540 suggestion,
1541 Applicability::MachineApplicable,
1542 );
1543 true
1544 }
1545
1546 #[instrument(level = "trace", skip(self, err, provided_expr))]
1548 pub(crate) fn suggest_deref_unwrap_or(
1549 &self,
1550 err: &mut Diag<'_>,
1551 callee_ty: Option<Ty<'tcx>>,
1552 call_ident: Option<Ident>,
1553 expected_ty: Ty<'tcx>,
1554 provided_ty: Ty<'tcx>,
1555 provided_expr: &Expr<'tcx>,
1556 is_method: bool,
1557 ) {
1558 if !is_method {
1559 return;
1560 }
1561 let Some(callee_ty) = callee_ty else {
1562 return;
1563 };
1564 let ty::Adt(callee_adt, _) = callee_ty.peel_refs().kind() else {
1565 return;
1566 };
1567 let adt_name = if self.tcx.is_diagnostic_item(sym::Option, callee_adt.did()) {
1568 "Option"
1569 } else if self.tcx.is_diagnostic_item(sym::Result, callee_adt.did()) {
1570 "Result"
1571 } else {
1572 return;
1573 };
1574
1575 let Some(call_ident) = call_ident else {
1576 return;
1577 };
1578 if call_ident.name != sym::unwrap_or {
1579 return;
1580 }
1581
1582 let ty::Ref(_, peeled, _mutability) = provided_ty.kind() else {
1583 return;
1584 };
1585
1586 let dummy_ty = if let ty::Array(elem_ty, size) = peeled.kind()
1590 && let ty::Infer(_) = elem_ty.kind()
1591 && self
1592 .try_structurally_resolve_const(provided_expr.span, *size)
1593 .try_to_target_usize(self.tcx)
1594 == Some(0)
1595 {
1596 let slice = Ty::new_slice(self.tcx, *elem_ty);
1597 Ty::new_imm_ref(self.tcx, self.tcx.lifetimes.re_static, slice)
1598 } else {
1599 provided_ty
1600 };
1601
1602 if !self.may_coerce(expected_ty, dummy_ty) {
1603 return;
1604 }
1605 let msg = format!("use `{adt_name}::map_or` to deref inner value of `{adt_name}`");
1606 err.multipart_suggestion_verbose(
1607 msg,
1608 vec![
1609 (call_ident.span, "map_or".to_owned()),
1610 (provided_expr.span.shrink_to_hi(), ", |v| v".to_owned()),
1611 ],
1612 Applicability::MachineApplicable,
1613 );
1614 }
1615
1616 pub(crate) fn suggest_block_to_brackets(
1619 &self,
1620 diag: &mut Diag<'_>,
1621 blk: &hir::Block<'_>,
1622 blk_ty: Ty<'tcx>,
1623 expected_ty: Ty<'tcx>,
1624 ) {
1625 if let ty::Slice(elem_ty) | ty::Array(elem_ty, _) = expected_ty.kind() {
1626 if self.may_coerce(blk_ty, *elem_ty)
1627 && blk.stmts.is_empty()
1628 && blk.rules == hir::BlockCheckMode::DefaultBlock
1629 && let source_map = self.tcx.sess.source_map()
1630 && let Ok(snippet) = source_map.span_to_snippet(blk.span)
1631 && snippet.starts_with('{')
1632 && snippet.ends_with('}')
1633 {
1634 diag.multipart_suggestion_verbose(
1635 "to create an array, use square brackets instead of curly braces",
1636 vec![
1637 (
1638 blk.span
1639 .shrink_to_lo()
1640 .with_hi(rustc_span::BytePos(blk.span.lo().0 + 1)),
1641 "[".to_string(),
1642 ),
1643 (
1644 blk.span
1645 .shrink_to_hi()
1646 .with_lo(rustc_span::BytePos(blk.span.hi().0 - 1)),
1647 "]".to_string(),
1648 ),
1649 ],
1650 Applicability::MachineApplicable,
1651 );
1652 }
1653 }
1654 }
1655
1656 #[instrument(skip(self, err))]
1657 pub(crate) fn suggest_floating_point_literal(
1658 &self,
1659 err: &mut Diag<'_>,
1660 expr: &hir::Expr<'_>,
1661 expected_ty: Ty<'tcx>,
1662 ) -> bool {
1663 if !expected_ty.is_floating_point() {
1664 return false;
1665 }
1666 match expr.kind {
1667 ExprKind::Struct(&qpath, [start, end], _)
1668 if is_range_literal(expr)
1669 && self.tcx.qpath_is_lang_item(qpath, LangItem::Range) =>
1670 {
1671 err.span_suggestion_verbose(
1672 start.expr.span.shrink_to_hi().with_hi(end.expr.span.lo()),
1673 "remove the unnecessary `.` operator for a floating point literal",
1674 '.',
1675 Applicability::MaybeIncorrect,
1676 );
1677 true
1678 }
1679 ExprKind::Struct(&qpath, [arg], _)
1680 if is_range_literal(expr)
1681 && let Some(qpath @ (LangItem::RangeFrom | LangItem::RangeTo)) =
1682 self.tcx.qpath_lang_item(qpath) =>
1683 {
1684 let range_span = expr.span.parent_callsite().unwrap();
1685 match qpath {
1686 LangItem::RangeFrom => {
1687 err.span_suggestion_verbose(
1688 range_span.with_lo(arg.expr.span.hi()),
1689 "remove the unnecessary `.` operator for a floating point literal",
1690 '.',
1691 Applicability::MaybeIncorrect,
1692 );
1693 }
1694 _ => {
1695 err.span_suggestion_verbose(
1696 range_span.until(arg.expr.span),
1697 "remove the unnecessary `.` operator and add an integer part for a floating point literal",
1698 "0.",
1699 Applicability::MaybeIncorrect,
1700 );
1701 }
1702 }
1703 true
1704 }
1705 ExprKind::Lit(Spanned {
1706 node: rustc_ast::LitKind::Int(lit, rustc_ast::LitIntType::Unsuffixed),
1707 span,
1708 }) => {
1709 let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(span) else {
1710 return false;
1711 };
1712 if !(snippet.starts_with("0x") || snippet.starts_with("0X")) {
1713 return false;
1714 }
1715 if snippet.len() <= 5 || !snippet.is_char_boundary(snippet.len() - 3) {
1716 return false;
1717 }
1718 let (_, suffix) = snippet.split_at(snippet.len() - 3);
1719 let value = match suffix {
1720 "f32" => (lit.get() - 0xf32) / (16 * 16 * 16),
1721 "f64" => (lit.get() - 0xf64) / (16 * 16 * 16),
1722 _ => return false,
1723 };
1724 err.span_suggestions(
1725 expr.span,
1726 "rewrite this as a decimal floating point literal, or use `as` to turn a hex literal into a float",
1727 [format!("0x{value:X} as {suffix}"), format!("{value}_{suffix}")],
1728 Applicability::MaybeIncorrect,
1729 );
1730 true
1731 }
1732 _ => false,
1733 }
1734 }
1735
1736 #[instrument(skip(self, err))]
1739 pub(crate) fn suggest_null_ptr_for_literal_zero_given_to_ptr_arg(
1740 &self,
1741 err: &mut Diag<'_>,
1742 expr: &hir::Expr<'_>,
1743 expected_ty: Ty<'tcx>,
1744 ) -> bool {
1745 let ty::RawPtr(_, mutbl) = expected_ty.kind() else {
1747 return false;
1748 };
1749
1750 let ExprKind::Lit(Spanned { node: rustc_ast::LitKind::Int(Pu128(0), _), span }) = expr.kind
1752 else {
1753 return false;
1754 };
1755
1756 let null_sym = match mutbl {
1758 hir::Mutability::Not => sym::ptr_null,
1759 hir::Mutability::Mut => sym::ptr_null_mut,
1760 };
1761 let Some(null_did) = self.tcx.get_diagnostic_item(null_sym) else {
1762 return false;
1763 };
1764 let null_path_str = with_no_trimmed_paths!(self.tcx.def_path_str(null_did));
1765
1766 err.span_suggestion(
1768 span,
1769 format!("if you meant to create a null pointer, use `{null_path_str}()`"),
1770 null_path_str + "()",
1771 Applicability::MachineApplicable,
1772 );
1773
1774 true
1775 }
1776
1777 pub(crate) fn suggest_associated_const(
1778 &self,
1779 err: &mut Diag<'_>,
1780 expr: &hir::Expr<'tcx>,
1781 expected_ty: Ty<'tcx>,
1782 ) -> bool {
1783 let Some((DefKind::AssocFn, old_def_id)) =
1784 self.typeck_results.borrow().type_dependent_def(expr.hir_id)
1785 else {
1786 return false;
1787 };
1788 let old_item_name = self.tcx.item_name(old_def_id);
1789 let capitalized_name = Symbol::intern(&old_item_name.as_str().to_uppercase());
1790 if old_item_name == capitalized_name {
1791 return false;
1792 }
1793 let (item, segment) = match expr.kind {
1794 hir::ExprKind::Path(QPath::Resolved(
1795 Some(ty),
1796 hir::Path { segments: [segment], .. },
1797 ))
1798 | hir::ExprKind::Path(QPath::TypeRelative(ty, segment))
1799 if let Some(self_ty) = self.typeck_results.borrow().node_type_opt(ty.hir_id)
1800 && let Ok(pick) = self.probe_for_name(
1801 Mode::Path,
1802 Ident::new(capitalized_name, segment.ident.span),
1803 Some(expected_ty),
1804 IsSuggestion(true),
1805 self_ty,
1806 expr.hir_id,
1807 ProbeScope::TraitsInScope,
1808 ) =>
1809 {
1810 (pick.item, segment)
1811 }
1812 hir::ExprKind::Path(QPath::Resolved(
1813 None,
1814 hir::Path { segments: [.., segment], .. },
1815 )) => {
1816 if old_item_name != segment.ident.name {
1819 return false;
1820 }
1821 let Some(item) = self
1822 .tcx
1823 .associated_items(self.tcx.parent(old_def_id))
1824 .filter_by_name_unhygienic(capitalized_name)
1825 .next()
1826 else {
1827 return false;
1828 };
1829 (*item, segment)
1830 }
1831 _ => return false,
1832 };
1833 if item.def_id == old_def_id || self.tcx.def_kind(item.def_id) != DefKind::AssocConst {
1834 return false;
1836 }
1837 let item_ty = self.tcx.type_of(item.def_id).instantiate_identity();
1838 if item_ty.has_param() {
1840 return false;
1841 }
1842 if self.may_coerce(item_ty, expected_ty) {
1843 err.span_suggestion_verbose(
1844 segment.ident.span,
1845 format!("try referring to the associated const `{capitalized_name}` instead",),
1846 capitalized_name,
1847 Applicability::MachineApplicable,
1848 );
1849 true
1850 } else {
1851 false
1852 }
1853 }
1854
1855 fn is_loop(&self, id: HirId) -> bool {
1856 let node = self.tcx.hir_node(id);
1857 matches!(node, Node::Expr(Expr { kind: ExprKind::Loop(..), .. }))
1858 }
1859
1860 fn is_local_statement(&self, id: HirId) -> bool {
1861 let node = self.tcx.hir_node(id);
1862 matches!(node, Node::Stmt(Stmt { kind: StmtKind::Let(..), .. }))
1863 }
1864
1865 pub(crate) fn note_type_is_not_clone(
1868 &self,
1869 diag: &mut Diag<'_>,
1870 expected_ty: Ty<'tcx>,
1871 found_ty: Ty<'tcx>,
1872 expr: &hir::Expr<'_>,
1873 ) {
1874 let expr = self.note_type_is_not_clone_inner_expr(expr);
1877
1878 let hir::ExprKind::MethodCall(segment, callee_expr, &[], _) = expr.kind else {
1880 return;
1881 };
1882
1883 let Some(clone_trait_did) = self.tcx.lang_items().clone_trait() else {
1884 return;
1885 };
1886 let ty::Ref(_, pointee_ty, _) = found_ty.kind() else { return };
1887 let results = self.typeck_results.borrow();
1888 if segment.ident.name == sym::clone
1890 && results.type_dependent_def_id(expr.hir_id).is_some_and(|did| {
1891 let assoc_item = self.tcx.associated_item(did);
1892 assoc_item.container == ty::AssocContainer::Trait
1893 && assoc_item.container_id(self.tcx) == clone_trait_did
1894 })
1895 && !results.expr_adjustments(callee_expr).iter().any(|adj| matches!(adj.kind, ty::adjustment::Adjust::Deref(..)))
1898 && self.may_coerce(*pointee_ty, expected_ty)
1900 && let trait_ref = ty::TraitRef::new(self.tcx, clone_trait_did, [expected_ty])
1901 && !self.predicate_must_hold_considering_regions(&traits::Obligation::new(
1903 self.tcx,
1904 traits::ObligationCause::dummy(),
1905 self.param_env,
1906 trait_ref,
1907 ))
1908 {
1909 diag.span_note(
1910 callee_expr.span,
1911 format!(
1912 "`{expected_ty}` does not implement `Clone`, so `{found_ty}` was cloned instead"
1913 ),
1914 );
1915 let owner = self.tcx.hir_enclosing_body_owner(expr.hir_id);
1916 if let ty::Param(param) = expected_ty.kind()
1917 && let Some(generics) = self.tcx.hir_get_generics(owner)
1918 {
1919 suggest_constraining_type_params(
1920 self.tcx,
1921 generics,
1922 diag,
1923 vec![(param.name.as_str(), "Clone", Some(clone_trait_did))].into_iter(),
1924 None,
1925 );
1926 } else {
1927 if let Some(errors) =
1928 self.type_implements_trait_shallow(clone_trait_did, expected_ty, self.param_env)
1929 {
1930 match &errors[..] {
1931 [] => {}
1932 [error] => {
1933 diag.help(format!(
1934 "`Clone` is not implemented because the trait bound `{}` is \
1935 not satisfied",
1936 error.obligation.predicate,
1937 ));
1938 }
1939 _ => {
1940 diag.help(format!(
1941 "`Clone` is not implemented because the following trait bounds \
1942 could not be satisfied: {}",
1943 listify(&errors, |e| format!("`{}`", e.obligation.predicate))
1944 .unwrap(),
1945 ));
1946 }
1947 }
1948 for error in errors {
1949 if let traits::FulfillmentErrorCode::Select(
1950 traits::SelectionError::Unimplemented,
1951 ) = error.code
1952 && let ty::PredicateKind::Clause(ty::ClauseKind::Trait(pred)) =
1953 error.obligation.predicate.kind().skip_binder()
1954 {
1955 self.infcx.err_ctxt().suggest_derive(
1956 &error.obligation,
1957 diag,
1958 error.obligation.predicate.kind().rebind(pred),
1959 );
1960 }
1961 }
1962 }
1963 self.suggest_derive(diag, &vec![(trait_ref.upcast(self.tcx), None, None)]);
1964 }
1965 }
1966 }
1967
1968 fn note_type_is_not_clone_inner_expr<'b>(
1973 &'b self,
1974 expr: &'b hir::Expr<'b>,
1975 ) -> &'b hir::Expr<'b> {
1976 match expr.peel_blocks().kind {
1977 hir::ExprKind::Path(hir::QPath::Resolved(
1978 None,
1979 hir::Path { segments: [_], res: crate::Res::Local(binding), .. },
1980 )) => {
1981 let hir::Node::Pat(hir::Pat { hir_id, .. }) = self.tcx.hir_node(*binding) else {
1982 return expr;
1983 };
1984
1985 match self.tcx.parent_hir_node(*hir_id) {
1986 hir::Node::LetStmt(hir::LetStmt { init: Some(init), .. }) => {
1988 self.note_type_is_not_clone_inner_expr(init)
1989 }
1990 hir::Node::Pat(hir::Pat {
1992 hir_id: pat_hir_id,
1993 kind: hir::PatKind::Tuple(pats, ..),
1994 ..
1995 }) => {
1996 let hir::Node::LetStmt(hir::LetStmt { init: Some(init), .. }) =
1997 self.tcx.parent_hir_node(*pat_hir_id)
1998 else {
1999 return expr;
2000 };
2001
2002 match init.peel_blocks().kind {
2003 ExprKind::Tup(init_tup) => {
2004 if let Some(init) = pats
2005 .iter()
2006 .enumerate()
2007 .filter(|x| x.1.hir_id == *hir_id)
2008 .find_map(|(i, _)| init_tup.get(i))
2009 {
2010 self.note_type_is_not_clone_inner_expr(init)
2011 } else {
2012 expr
2013 }
2014 }
2015 _ => expr,
2016 }
2017 }
2018 _ => expr,
2019 }
2020 }
2021 hir::ExprKind::Call(Expr { kind: call_expr_kind, .. }, _) => {
2025 if let hir::ExprKind::Path(hir::QPath::Resolved(None, call_expr_path)) =
2026 call_expr_kind
2027 && let hir::Path { segments: [_], res: crate::Res::Local(binding), .. } =
2028 call_expr_path
2029 && let hir::Node::Pat(hir::Pat { hir_id, .. }) = self.tcx.hir_node(*binding)
2030 && let hir::Node::LetStmt(hir::LetStmt { init: Some(init), .. }) =
2031 self.tcx.parent_hir_node(*hir_id)
2032 && let Expr {
2033 kind: hir::ExprKind::Closure(hir::Closure { body: body_id, .. }),
2034 ..
2035 } = init
2036 {
2037 let hir::Body { value: body_expr, .. } = self.tcx.hir_body(*body_id);
2038 self.note_type_is_not_clone_inner_expr(body_expr)
2039 } else {
2040 expr
2041 }
2042 }
2043 _ => expr,
2044 }
2045 }
2046
2047 pub(crate) fn is_field_suggestable(
2048 &self,
2049 field: &ty::FieldDef,
2050 hir_id: HirId,
2051 span: Span,
2052 ) -> bool {
2053 field.vis.is_accessible_from(self.tcx.parent_module(hir_id), self.tcx)
2055 && !matches!(
2057 self.tcx.eval_stability(field.did, None, rustc_span::DUMMY_SP, None),
2058 rustc_middle::middle::stability::EvalResult::Deny { .. }
2059 )
2060 && (field.did.is_local() || !self.tcx.is_doc_hidden(field.did))
2062 && self.tcx.def_ident_span(field.did).unwrap().normalize_to_macros_2_0().eq_ctxt(span)
2064 }
2065
2066 pub(crate) fn suggest_missing_unwrap_expect(
2067 &self,
2068 err: &mut Diag<'_>,
2069 expr: &hir::Expr<'tcx>,
2070 expected: Ty<'tcx>,
2071 found: Ty<'tcx>,
2072 ) -> bool {
2073 let ty::Adt(adt, args) = found.kind() else {
2074 return false;
2075 };
2076 let ret_ty_matches = |diagnostic_item| {
2077 let Some(sig) = self.body_fn_sig() else {
2078 return false;
2079 };
2080 let ty::Adt(kind, _) = sig.output().kind() else {
2081 return false;
2082 };
2083 self.tcx.is_diagnostic_item(diagnostic_item, kind.did())
2084 };
2085
2086 let is_ctor = matches!(
2089 expr.kind,
2090 hir::ExprKind::Call(
2091 hir::Expr {
2092 kind: hir::ExprKind::Path(hir::QPath::Resolved(
2093 None,
2094 hir::Path { res: Res::Def(hir::def::DefKind::Ctor(_, _), _), .. },
2095 )),
2096 ..
2097 },
2098 ..,
2099 ) | hir::ExprKind::Path(hir::QPath::Resolved(
2100 None,
2101 hir::Path { res: Res::Def(hir::def::DefKind::Ctor(_, _), _), .. },
2102 )),
2103 );
2104
2105 let (article, kind, variant, sugg_operator) = if self.tcx.is_diagnostic_item(sym::Result, adt.did())
2106 && !self.tcx.hir_is_inside_const_context(expr.hir_id)
2108 {
2109 ("a", "Result", "Err", ret_ty_matches(sym::Result))
2110 } else if self.tcx.is_diagnostic_item(sym::Option, adt.did()) {
2111 ("an", "Option", "None", ret_ty_matches(sym::Option))
2112 } else {
2113 return false;
2114 };
2115 if is_ctor || !self.may_coerce(args.type_at(0), expected) {
2116 return false;
2117 }
2118
2119 let (msg, sugg) = if sugg_operator {
2120 (
2121 format!(
2122 "use the `?` operator to extract the `{found}` value, propagating \
2123 {article} `{kind}::{variant}` value to the caller"
2124 ),
2125 "?",
2126 )
2127 } else {
2128 (
2129 format!(
2130 "consider using `{kind}::expect` to unwrap the `{found}` value, \
2131 panicking if the value is {article} `{kind}::{variant}`"
2132 ),
2133 ".expect(\"REASON\")",
2134 )
2135 };
2136
2137 let sugg = match self.tcx.hir_maybe_get_struct_pattern_shorthand_field(expr) {
2138 Some(_) if expr.span.from_expansion() => return false,
2139 Some(ident) => format!(": {ident}{sugg}"),
2140 None => sugg.to_string(),
2141 };
2142
2143 let span = expr
2144 .span
2145 .find_ancestor_not_from_extern_macro(self.tcx.sess.source_map())
2146 .unwrap_or(expr.span);
2147 err.span_suggestion_verbose(span.shrink_to_hi(), msg, sugg, Applicability::HasPlaceholders);
2148 true
2149 }
2150
2151 pub(crate) fn suggest_coercing_result_via_try_operator(
2152 &self,
2153 err: &mut Diag<'_>,
2154 expr: &hir::Expr<'tcx>,
2155 expected: Ty<'tcx>,
2156 found: Ty<'tcx>,
2157 ) -> bool {
2158 let returned = matches!(
2159 self.tcx.parent_hir_node(expr.hir_id),
2160 hir::Node::Expr(hir::Expr { kind: hir::ExprKind::Ret(_), .. })
2161 ) || self.tcx.hir_get_fn_id_for_return_block(expr.hir_id).is_some();
2162 if returned
2163 && let ty::Adt(e, args_e) = expected.kind()
2164 && let ty::Adt(f, args_f) = found.kind()
2165 && e.did() == f.did()
2166 && Some(e.did()) == self.tcx.get_diagnostic_item(sym::Result)
2167 && let e_ok = args_e.type_at(0)
2168 && let f_ok = args_f.type_at(0)
2169 && self.infcx.can_eq(self.param_env, f_ok, e_ok)
2170 && let e_err = args_e.type_at(1)
2171 && let f_err = args_f.type_at(1)
2172 && self
2173 .infcx
2174 .type_implements_trait(
2175 self.tcx.get_diagnostic_item(sym::Into).unwrap(),
2176 [f_err, e_err],
2177 self.param_env,
2178 )
2179 .must_apply_modulo_regions()
2180 {
2181 err.multipart_suggestion(
2182 "use `?` to coerce and return an appropriate `Err`, and wrap the resulting value \
2183 in `Ok` so the expression remains of type `Result`",
2184 vec![
2185 (expr.span.shrink_to_lo(), "Ok(".to_string()),
2186 (expr.span.shrink_to_hi(), "?)".to_string()),
2187 ],
2188 Applicability::MaybeIncorrect,
2189 );
2190 return true;
2191 }
2192 false
2193 }
2194
2195 pub(crate) fn suggest_returning_value_after_loop(
2198 &self,
2199 err: &mut Diag<'_>,
2200 expr: &hir::Expr<'tcx>,
2201 expected: Ty<'tcx>,
2202 ) -> bool {
2203 let tcx = self.tcx;
2204 let enclosing_scope =
2205 tcx.hir_get_enclosing_scope(expr.hir_id).map(|hir_id| tcx.hir_node(hir_id));
2206
2207 let tail_expr = if let Some(Node::Block(hir::Block { expr, .. })) = enclosing_scope
2209 && expr.is_some()
2210 {
2211 *expr
2212 } else {
2213 let body_def_id = tcx.hir_enclosing_body_owner(expr.hir_id);
2214 let body = tcx.hir_body_owned_by(body_def_id);
2215
2216 match body.value.kind {
2218 hir::ExprKind::Block(block, _) => block.expr,
2220 hir::ExprKind::DropTemps(expr) => Some(expr),
2222 _ => None,
2223 }
2224 };
2225
2226 let Some(tail_expr) = tail_expr else {
2227 return false; };
2229
2230 let loop_expr_in_tail = match expr.kind {
2232 hir::ExprKind::Loop(_, _, hir::LoopSource::While, _) => tail_expr,
2233 hir::ExprKind::Loop(_, _, hir::LoopSource::ForLoop, _) => {
2234 match tail_expr.peel_drop_temps() {
2235 Expr { kind: ExprKind::Match(_, [Arm { body, .. }], _), .. } => body,
2236 _ => return false, }
2238 }
2239 _ => return false, };
2241
2242 if expr.hir_id == loop_expr_in_tail.hir_id {
2245 let span = expr.span;
2246
2247 let (msg, suggestion) = if expected.is_never() {
2248 (
2249 "consider adding a diverging expression here",
2250 "`loop {}` or `panic!(\"...\")`".to_string(),
2251 )
2252 } else {
2253 ("consider returning a value here", format!("`{expected}` value"))
2254 };
2255
2256 let src_map = tcx.sess.source_map();
2257 let suggestion = if src_map.is_multiline(expr.span) {
2258 let indentation = src_map.indentation_before(span).unwrap_or_default();
2259 format!("\n{indentation}/* {suggestion} */")
2260 } else {
2261 format!(" /* {suggestion} */")
2264 };
2265
2266 err.span_suggestion_verbose(
2267 span.shrink_to_hi(),
2268 msg,
2269 suggestion,
2270 Applicability::MaybeIncorrect,
2271 );
2272
2273 true
2274 } else {
2275 false
2276 }
2277 }
2278
2279 pub(crate) fn suggest_semicolon_in_repeat_expr(
2282 &self,
2283 err: &mut Diag<'_>,
2284 expr: &hir::Expr<'_>,
2285 expr_ty: Ty<'tcx>,
2286 ) -> bool {
2287 if let hir::Node::Expr(array_expr) = self.tcx.parent_hir_node(expr.hir_id)
2289 && let hir::ExprKind::Array(elements) = array_expr.kind
2290 && let [first, second] = elements
2291 && second.hir_id == expr.hir_id
2292 {
2293 let comma_span = first.span.between(second.span);
2295
2296 let expr_is_const_usize = expr_ty.is_usize()
2304 && match expr.kind {
2305 ExprKind::Path(QPath::Resolved(
2306 None,
2307 Path { res: Res::Def(DefKind::Const, _), .. },
2308 )) => true,
2309 ExprKind::Call(
2310 Expr {
2311 kind:
2312 ExprKind::Path(QPath::Resolved(
2313 None,
2314 Path { res: Res::Def(DefKind::Fn, fn_def_id), .. },
2315 )),
2316 ..
2317 },
2318 _,
2319 ) => self.tcx.is_const_fn(*fn_def_id),
2320 _ => false,
2321 };
2322
2323 let first_ty = self.typeck_results.borrow().expr_ty(first);
2328
2329 if self.tcx.sess.source_map().is_imported(array_expr.span)
2334 && self.type_is_clone_modulo_regions(self.param_env, first_ty)
2335 && (expr.is_size_lit() || expr_ty.is_usize_like())
2336 {
2337 err.subdiagnostic(errors::ReplaceCommaWithSemicolon {
2338 comma_span,
2339 descr: "a vector",
2340 });
2341 return true;
2342 }
2343
2344 if self.type_is_copy_modulo_regions(self.param_env, first_ty)
2348 && (expr.is_size_lit() || expr_is_const_usize)
2349 {
2350 err.subdiagnostic(errors::ReplaceCommaWithSemicolon {
2351 comma_span,
2352 descr: "an array",
2353 });
2354 return true;
2355 }
2356 }
2357 false
2358 }
2359
2360 pub(crate) fn suggest_compatible_variants(
2363 &self,
2364 err: &mut Diag<'_>,
2365 expr: &hir::Expr<'_>,
2366 expected: Ty<'tcx>,
2367 expr_ty: Ty<'tcx>,
2368 ) -> bool {
2369 if expr.span.in_external_macro(self.tcx.sess.source_map()) {
2370 return false;
2371 }
2372 if let ty::Adt(expected_adt, args) = expected.kind() {
2373 if let hir::ExprKind::Field(base, ident) = expr.kind {
2374 let base_ty = self.typeck_results.borrow().expr_ty(base);
2375 if self.can_eq(self.param_env, base_ty, expected)
2376 && let Some(base_span) = base.span.find_ancestor_inside(expr.span)
2377 {
2378 err.span_suggestion_verbose(
2379 expr.span.with_lo(base_span.hi()),
2380 format!("consider removing the tuple struct field `{ident}`"),
2381 "",
2382 Applicability::MaybeIncorrect,
2383 );
2384 return true;
2385 }
2386 }
2387
2388 if expr_ty.is_unit() {
2392 let mut id = expr.hir_id;
2393 let mut parent;
2394
2395 loop {
2397 parent = self.tcx.parent_hir_id(id);
2398 let parent_span = self.tcx.hir_span(parent);
2399 if parent_span.find_ancestor_inside(expr.span).is_some() {
2400 id = parent;
2403 continue;
2404 }
2405 break;
2406 }
2407
2408 if let hir::Node::Block(&hir::Block { span: block_span, expr: Some(e), .. }) =
2409 self.tcx.hir_node(parent)
2410 {
2411 if e.hir_id == id {
2412 if let Some(span) = expr.span.find_ancestor_inside(block_span) {
2413 let return_suggestions = if self
2414 .tcx
2415 .is_diagnostic_item(sym::Result, expected_adt.did())
2416 {
2417 vec!["Ok(())"]
2418 } else if self.tcx.is_diagnostic_item(sym::Option, expected_adt.did()) {
2419 vec!["None", "Some(())"]
2420 } else {
2421 return false;
2422 };
2423 if let Some(indent) =
2424 self.tcx.sess.source_map().indentation_before(span.shrink_to_lo())
2425 {
2426 let semicolon =
2428 match self.tcx.sess.source_map().span_to_snippet(span) {
2429 Ok(s) if s.ends_with('}') => "",
2430 _ => ";",
2431 };
2432 err.span_suggestions(
2433 span.shrink_to_hi(),
2434 "try adding an expression at the end of the block",
2435 return_suggestions
2436 .into_iter()
2437 .map(|r| format!("{semicolon}\n{indent}{r}")),
2438 Applicability::MaybeIncorrect,
2439 );
2440 }
2441 return true;
2442 }
2443 }
2444 }
2445 }
2446
2447 let compatible_variants: Vec<(String, _, _, Option<String>)> = expected_adt
2448 .variants()
2449 .iter()
2450 .filter(|variant| {
2451 variant.fields.len() == 1
2452 })
2453 .filter_map(|variant| {
2454 let sole_field = &variant.single_field();
2455
2456 if let (ty::Adt(exp_adt, _), ty::Adt(act_adt, _)) = (expected.kind(), expr_ty.kind())
2460 && exp_adt.did() == act_adt.did()
2461 && sole_field.ty(self.tcx, args).is_ty_var() {
2462 return None;
2463 }
2464
2465 let field_is_local = sole_field.did.is_local();
2466 let field_is_accessible =
2467 sole_field.vis.is_accessible_from(expr.hir_id.owner.def_id, self.tcx)
2468 && matches!(self.tcx.eval_stability(sole_field.did, None, expr.span, None), EvalResult::Allow | EvalResult::Unmarked);
2470
2471 if !field_is_local && !field_is_accessible {
2472 return None;
2473 }
2474
2475 let note_about_variant_field_privacy = (field_is_local && !field_is_accessible)
2476 .then(|| " (its field is private, but it's local to this crate and its privacy can be changed)".to_string());
2477
2478 let sole_field_ty = sole_field.ty(self.tcx, args);
2479 if self.may_coerce(expr_ty, sole_field_ty) {
2480 let variant_path =
2481 with_no_trimmed_paths!(self.tcx.def_path_str(variant.def_id));
2482 if let Some(path) = variant_path.strip_prefix("std::prelude::")
2484 && let Some((_, path)) = path.split_once("::")
2485 {
2486 return Some((path.to_string(), variant.ctor_kind(), sole_field.name, note_about_variant_field_privacy));
2487 }
2488 Some((variant_path, variant.ctor_kind(), sole_field.name, note_about_variant_field_privacy))
2489 } else {
2490 None
2491 }
2492 })
2493 .collect();
2494
2495 let suggestions_for = |variant: &_, ctor_kind, field_name| {
2496 let prefix = match self.tcx.hir_maybe_get_struct_pattern_shorthand_field(expr) {
2497 Some(ident) => format!("{ident}: "),
2498 None => String::new(),
2499 };
2500
2501 let (open, close) = match ctor_kind {
2502 Some(CtorKind::Fn) => ("(".to_owned(), ")"),
2503 None => (format!(" {{ {field_name}: "), " }"),
2504
2505 Some(CtorKind::Const) => unreachable!("unit variants don't have fields"),
2506 };
2507
2508 let mut expr = expr;
2512 while let hir::ExprKind::Block(block, _) = &expr.kind
2513 && let Some(expr_) = &block.expr
2514 {
2515 expr = expr_
2516 }
2517
2518 vec![
2519 (expr.span.shrink_to_lo(), format!("{prefix}{variant}{open}")),
2520 (expr.span.shrink_to_hi(), close.to_owned()),
2521 ]
2522 };
2523
2524 match &compatible_variants[..] {
2525 [] => { }
2526 [(variant, ctor_kind, field_name, note)] => {
2527 err.multipart_suggestion_verbose(
2529 format!(
2530 "try wrapping the expression in `{variant}`{note}",
2531 note = note.as_deref().unwrap_or("")
2532 ),
2533 suggestions_for(&**variant, *ctor_kind, *field_name),
2534 Applicability::MaybeIncorrect,
2535 );
2536 return true;
2537 }
2538 _ => {
2539 err.multipart_suggestions(
2541 format!(
2542 "try wrapping the expression in a variant of `{}`",
2543 self.tcx.def_path_str(expected_adt.did())
2544 ),
2545 compatible_variants.into_iter().map(
2546 |(variant, ctor_kind, field_name, _)| {
2547 suggestions_for(&variant, ctor_kind, field_name)
2548 },
2549 ),
2550 Applicability::MaybeIncorrect,
2551 );
2552 return true;
2553 }
2554 }
2555 }
2556
2557 false
2558 }
2559
2560 pub(crate) fn suggest_non_zero_new_unwrap(
2561 &self,
2562 err: &mut Diag<'_>,
2563 expr: &hir::Expr<'_>,
2564 expected: Ty<'tcx>,
2565 expr_ty: Ty<'tcx>,
2566 ) -> bool {
2567 let tcx = self.tcx;
2568 let (adt, args, unwrap) = match expected.kind() {
2569 ty::Adt(adt, args) if tcx.is_diagnostic_item(sym::Option, adt.did()) => {
2571 let nonzero_type = args.type_at(0); let ty::Adt(adt, args) = nonzero_type.kind() else {
2573 return false;
2574 };
2575 (adt, args, "")
2576 }
2577 ty::Adt(adt, args) => (adt, args, ".unwrap()"),
2579 _ => return false,
2580 };
2581
2582 if !self.tcx.is_diagnostic_item(sym::NonZero, adt.did()) {
2583 return false;
2584 }
2585
2586 let int_type = args.type_at(0);
2587 if !self.may_coerce(expr_ty, int_type) {
2588 return false;
2589 }
2590
2591 err.multipart_suggestion(
2592 format!("consider calling `{}::new`", sym::NonZero),
2593 vec![
2594 (expr.span.shrink_to_lo(), format!("{}::new(", sym::NonZero)),
2595 (expr.span.shrink_to_hi(), format!("){unwrap}")),
2596 ],
2597 Applicability::MaybeIncorrect,
2598 );
2599
2600 true
2601 }
2602
2603 fn can_use_as_ref(&self, expr: &hir::Expr<'_>) -> Option<(Vec<(Span, String)>, &'static str)> {
2620 let hir::ExprKind::Path(hir::QPath::Resolved(_, path)) = expr.kind else {
2621 return None;
2622 };
2623
2624 let hir::def::Res::Local(local_id) = path.res else {
2625 return None;
2626 };
2627
2628 let Node::Param(hir::Param { hir_id: param_hir_id, .. }) =
2629 self.tcx.parent_hir_node(local_id)
2630 else {
2631 return None;
2632 };
2633
2634 let Node::Expr(hir::Expr {
2635 hir_id: expr_hir_id,
2636 kind: hir::ExprKind::Closure(hir::Closure { fn_decl: closure_fn_decl, .. }),
2637 ..
2638 }) = self.tcx.parent_hir_node(*param_hir_id)
2639 else {
2640 return None;
2641 };
2642
2643 let hir = self.tcx.parent_hir_node(*expr_hir_id);
2644 let closure_params_len = closure_fn_decl.inputs.len();
2645 let (
2646 Node::Expr(hir::Expr {
2647 kind: hir::ExprKind::MethodCall(method_path, receiver, ..),
2648 ..
2649 }),
2650 1,
2651 ) = (hir, closure_params_len)
2652 else {
2653 return None;
2654 };
2655
2656 let self_ty = self.typeck_results.borrow().expr_ty(receiver);
2657 let name = method_path.ident.name;
2658 let is_as_ref_able = match self_ty.peel_refs().kind() {
2659 ty::Adt(def, _) => {
2660 (self.tcx.is_diagnostic_item(sym::Option, def.did())
2661 || self.tcx.is_diagnostic_item(sym::Result, def.did()))
2662 && (name == sym::map || name == sym::and_then)
2663 }
2664 _ => false,
2665 };
2666 if is_as_ref_able {
2667 Some((
2668 vec![(method_path.ident.span.shrink_to_lo(), "as_ref().".to_string())],
2669 "consider using `as_ref` instead",
2670 ))
2671 } else {
2672 None
2673 }
2674 }
2675
2676 pub(crate) fn suggest_deref_or_ref(
2693 &self,
2694 expr: &hir::Expr<'tcx>,
2695 checked_ty: Ty<'tcx>,
2696 expected: Ty<'tcx>,
2697 ) -> Option<(
2698 Vec<(Span, String)>,
2699 String,
2700 Applicability,
2701 bool, bool, )> {
2704 let sess = self.sess();
2705 let sp = expr.range_span().unwrap_or(expr.span);
2706 let sm = sess.source_map();
2707
2708 if sp.in_external_macro(sm) {
2710 return None;
2711 }
2712
2713 let replace_prefix = |s: &str, old: &str, new: &str| {
2714 s.strip_prefix(old).map(|stripped| new.to_string() + stripped)
2715 };
2716
2717 let expr = expr.peel_drop_temps();
2719
2720 match (&expr.kind, expected.kind(), checked_ty.kind()) {
2721 (_, &ty::Ref(_, exp, _), &ty::Ref(_, check, _)) => match (exp.kind(), check.kind()) {
2722 (&ty::Str, &ty::Array(arr, _) | &ty::Slice(arr)) if arr == self.tcx.types.u8 => {
2723 if let hir::ExprKind::Lit(_) = expr.kind
2724 && let Ok(src) = sm.span_to_snippet(sp)
2725 && replace_prefix(&src, "b\"", "\"").is_some()
2726 {
2727 let pos = sp.lo() + BytePos(1);
2728 return Some((
2729 vec![(sp.with_hi(pos), String::new())],
2730 "consider removing the leading `b`".to_string(),
2731 Applicability::MachineApplicable,
2732 true,
2733 false,
2734 ));
2735 }
2736 }
2737 (&ty::Array(arr, _) | &ty::Slice(arr), &ty::Str) if arr == self.tcx.types.u8 => {
2738 if let hir::ExprKind::Lit(_) = expr.kind
2739 && let Ok(src) = sm.span_to_snippet(sp)
2740 && replace_prefix(&src, "\"", "b\"").is_some()
2741 {
2742 return Some((
2743 vec![(sp.shrink_to_lo(), "b".to_string())],
2744 "consider adding a leading `b`".to_string(),
2745 Applicability::MachineApplicable,
2746 true,
2747 false,
2748 ));
2749 }
2750 }
2751 _ => {}
2752 },
2753 (_, &ty::Ref(_, _, mutability), _) => {
2754 let ref_ty = match mutability {
2763 hir::Mutability::Mut => {
2764 Ty::new_mut_ref(self.tcx, self.tcx.lifetimes.re_static, checked_ty)
2765 }
2766 hir::Mutability::Not => {
2767 Ty::new_imm_ref(self.tcx, self.tcx.lifetimes.re_static, checked_ty)
2768 }
2769 };
2770 if self.may_coerce(ref_ty, expected) {
2771 let mut sugg_sp = sp;
2772 if let hir::ExprKind::MethodCall(segment, receiver, args, _) = expr.kind {
2773 let clone_trait =
2774 self.tcx.require_lang_item(LangItem::Clone, segment.ident.span);
2775 if args.is_empty()
2776 && self
2777 .typeck_results
2778 .borrow()
2779 .type_dependent_def_id(expr.hir_id)
2780 .is_some_and(|did| {
2781 let ai = self.tcx.associated_item(did);
2782 ai.trait_container(self.tcx) == Some(clone_trait)
2783 })
2784 && segment.ident.name == sym::clone
2785 {
2786 sugg_sp = receiver.span;
2789 }
2790 }
2791
2792 if let hir::ExprKind::Unary(hir::UnOp::Deref, inner) = expr.kind
2793 && let Some(1) = self.deref_steps_for_suggestion(expected, checked_ty)
2794 && self.typeck_results.borrow().expr_ty(inner).is_ref()
2795 {
2796 sugg_sp = sugg_sp.with_hi(inner.span.lo());
2799 return Some((
2800 vec![(sugg_sp, String::new())],
2801 "consider removing deref here".to_string(),
2802 Applicability::MachineApplicable,
2803 true,
2804 false,
2805 ));
2806 }
2807
2808 if let hir::ExprKind::If(_c, then, els) = expr.kind {
2814 let ExprKind::Block(then, _) = then.kind else { return None };
2817 let Some(then) = then.expr else { return None };
2818 let (mut suggs, help, app, verbose, mutref) =
2819 self.suggest_deref_or_ref(then, checked_ty, expected)?;
2820
2821 let els_expr = match els?.kind {
2823 ExprKind::Block(block, _) => block.expr?,
2824 _ => els?,
2825 };
2826 let (else_suggs, ..) =
2827 self.suggest_deref_or_ref(els_expr, checked_ty, expected)?;
2828 suggs.extend(else_suggs);
2829
2830 return Some((suggs, help, app, verbose, mutref));
2831 }
2832
2833 if let Some((sugg, msg)) = self.can_use_as_ref(expr) {
2834 return Some((
2835 sugg,
2836 msg.to_string(),
2837 Applicability::MachineApplicable,
2838 true,
2839 false,
2840 ));
2841 }
2842
2843 let prefix = match self.tcx.hir_maybe_get_struct_pattern_shorthand_field(expr) {
2844 Some(ident) => format!("{ident}: "),
2845 None => String::new(),
2846 };
2847
2848 if let hir::Node::Expr(hir::Expr { kind: hir::ExprKind::Assign(..), .. }) =
2849 self.tcx.parent_hir_node(expr.hir_id)
2850 {
2851 if mutability.is_mut() {
2852 return None;
2854 }
2855 }
2856
2857 let make_sugg = |expr: &Expr<'_>, span: Span, sugg: &str| {
2858 if expr_needs_parens(expr) {
2859 (
2860 vec![
2861 (span.shrink_to_lo(), format!("{prefix}{sugg}(")),
2862 (span.shrink_to_hi(), ")".to_string()),
2863 ],
2864 false,
2865 )
2866 } else {
2867 (vec![(span.shrink_to_lo(), format!("{prefix}{sugg}"))], true)
2868 }
2869 };
2870
2871 if let hir::Node::Expr(hir::Expr {
2873 kind: hir::ExprKind::Binary(_, lhs, ..),
2874 ..
2875 }) = self.tcx.parent_hir_node(expr.hir_id)
2876 && let &ty::Ref(..) = self.check_expr(lhs).kind()
2877 {
2878 let (sugg, verbose) = make_sugg(lhs, lhs.span, "*");
2879
2880 return Some((
2881 sugg,
2882 "consider dereferencing the borrow".to_string(),
2883 Applicability::MachineApplicable,
2884 verbose,
2885 false,
2886 ));
2887 }
2888
2889 let sugg = mutability.ref_prefix_str();
2890 let (sugg, verbose) = make_sugg(expr, sp, sugg);
2891 return Some((
2892 sugg,
2893 format!("consider {}borrowing here", mutability.mutably_str()),
2894 Applicability::MachineApplicable,
2895 verbose,
2896 false,
2897 ));
2898 }
2899 }
2900 (hir::ExprKind::AddrOf(hir::BorrowKind::Ref, _, expr), _, &ty::Ref(_, checked, _))
2901 if self.can_eq(self.param_env, checked, expected) =>
2902 {
2903 let make_sugg = |start: Span, end: BytePos| {
2904 let sp = sm
2907 .span_extend_while(start.shrink_to_lo(), |c| c == '(' || c.is_whitespace())
2908 .map_or(start, |s| s.shrink_to_hi());
2909 Some((
2910 vec![(sp.with_hi(end), String::new())],
2911 "consider removing the borrow".to_string(),
2912 Applicability::MachineApplicable,
2913 true,
2914 true,
2915 ))
2916 };
2917
2918 if sm.is_imported(expr.span) {
2921 if let Some(call_span) =
2927 iter::successors(Some(expr.span), |s| s.parent_callsite())
2928 .find(|&s| sp.contains(s))
2929 && sm.is_span_accessible(call_span)
2930 {
2931 return make_sugg(sp, call_span.lo());
2932 }
2933 return None;
2934 }
2935 if sp.contains(expr.span) && sm.is_span_accessible(expr.span) {
2936 return make_sugg(sp, expr.span.lo());
2937 }
2938 }
2939 (_, &ty::RawPtr(ty_b, mutbl_b), &ty::Ref(_, ty_a, mutbl_a)) => {
2940 if let Some(steps) = self.deref_steps_for_suggestion(ty_a, ty_b)
2941 && steps > 0
2943 && let Ok(src) = sm.span_to_snippet(sp)
2945 {
2946 let derefs = "*".repeat(steps);
2947 let old_prefix = mutbl_a.ref_prefix_str();
2948 let new_prefix = mutbl_b.ref_prefix_str().to_owned() + &derefs;
2949
2950 let suggestion = replace_prefix(&src, old_prefix, &new_prefix).map(|_| {
2951 let lo = sp.lo()
2953 + BytePos(min(old_prefix.len(), mutbl_b.ref_prefix_str().len()) as _);
2954 let hi = sp.lo() + BytePos(old_prefix.len() as _);
2956 let sp = sp.with_lo(lo).with_hi(hi);
2957
2958 (
2959 sp,
2960 format!(
2961 "{}{derefs}",
2962 if mutbl_a != mutbl_b { mutbl_b.prefix_str() } else { "" }
2963 ),
2964 if mutbl_b <= mutbl_a {
2965 Applicability::MachineApplicable
2966 } else {
2967 Applicability::MaybeIncorrect
2968 },
2969 )
2970 });
2971
2972 if let Some((span, src, applicability)) = suggestion {
2973 return Some((
2974 vec![(span, src)],
2975 "consider dereferencing".to_string(),
2976 applicability,
2977 true,
2978 false,
2979 ));
2980 }
2981 }
2982 }
2983 _ if sp == expr.span => {
2984 if let Some(mut steps) = self.deref_steps_for_suggestion(checked_ty, expected) {
2985 let mut expr = expr.peel_blocks();
2986 let mut prefix_span = expr.span.shrink_to_lo();
2987 let mut remove = String::new();
2988
2989 while steps > 0 {
2991 if let hir::ExprKind::AddrOf(_, mutbl, inner) = expr.kind {
2992 prefix_span = prefix_span.with_hi(inner.span.lo());
2994 expr = inner;
2995 remove.push_str(mutbl.ref_prefix_str());
2996 steps -= 1;
2997 } else {
2998 break;
2999 }
3000 }
3001 if steps == 0 && !remove.trim().is_empty() {
3003 return Some((
3004 vec![(prefix_span, String::new())],
3005 format!("consider removing the `{}`", remove.trim()),
3006 if remove.trim() == "&&" && expected == self.tcx.types.bool {
3010 Applicability::MaybeIncorrect
3011 } else {
3012 Applicability::MachineApplicable
3013 },
3014 true,
3015 false,
3016 ));
3017 }
3018
3019 if self.type_is_copy_modulo_regions(self.param_env, expected)
3022 || (checked_ty.is_box() && steps == 1)
3025 || matches!(
3027 self.tcx.parent_hir_node(expr.hir_id),
3028 hir::Node::Expr(hir::Expr { kind: hir::ExprKind::Binary(op, ..), .. })
3029 if !op.node.is_by_value()
3030 )
3031 {
3032 let deref_kind = if checked_ty.is_box() {
3033 if let ExprKind::Call(box_new, [_]) = expr.kind
3035 && let ExprKind::Path(qpath) = &box_new.kind
3036 && let Res::Def(DefKind::AssocFn, fn_id) =
3037 self.typeck_results.borrow().qpath_res(qpath, box_new.hir_id)
3038 && let Some(impl_id) = self.tcx.inherent_impl_of_assoc(fn_id)
3039 && self.tcx.type_of(impl_id).skip_binder().is_box()
3040 && self.tcx.item_name(fn_id) == sym::new
3041 {
3042 let l_paren = self.tcx.sess.source_map().next_point(box_new.span);
3043 let r_paren = self.tcx.sess.source_map().end_point(expr.span);
3044 return Some((
3045 vec![
3046 (box_new.span.to(l_paren), String::new()),
3047 (r_paren, String::new()),
3048 ],
3049 "consider removing the Box".to_string(),
3050 Applicability::MachineApplicable,
3051 false,
3052 false,
3053 ));
3054 }
3055 "unboxing the value"
3056 } else if checked_ty.is_ref() {
3057 "dereferencing the borrow"
3058 } else {
3059 "dereferencing the type"
3060 };
3061
3062 let message = if remove.is_empty() {
3065 format!("consider {deref_kind}")
3066 } else {
3067 format!(
3068 "consider removing the `{}` and {} instead",
3069 remove.trim(),
3070 deref_kind
3071 )
3072 };
3073
3074 let prefix =
3075 match self.tcx.hir_maybe_get_struct_pattern_shorthand_field(expr) {
3076 Some(ident) => format!("{ident}: "),
3077 None => String::new(),
3078 };
3079
3080 let (span, suggestion) = if self.is_else_if_block(expr) {
3081 return None;
3083 } else if let Some(expr) = self.maybe_get_block_expr(expr) {
3084 (expr.span.shrink_to_lo(), "*".to_string())
3086 } else {
3087 (prefix_span, format!("{}{}", prefix, "*".repeat(steps)))
3088 };
3089 if suggestion.trim().is_empty() {
3090 return None;
3091 }
3092
3093 if expr_needs_parens(expr) {
3094 return Some((
3095 vec![
3096 (span, format!("{suggestion}(")),
3097 (expr.span.shrink_to_hi(), ")".to_string()),
3098 ],
3099 message,
3100 Applicability::MachineApplicable,
3101 true,
3102 false,
3103 ));
3104 }
3105
3106 return Some((
3107 vec![(span, suggestion)],
3108 message,
3109 Applicability::MachineApplicable,
3110 true,
3111 false,
3112 ));
3113 }
3114 }
3115 }
3116 _ => {}
3117 }
3118 None
3119 }
3120
3121 fn is_else_if_block(&self, expr: &hir::Expr<'_>) -> bool {
3123 if let hir::ExprKind::If(..) = expr.kind
3124 && let Node::Expr(hir::Expr { kind: hir::ExprKind::If(_, _, Some(else_expr)), .. }) =
3125 self.tcx.parent_hir_node(expr.hir_id)
3126 {
3127 return else_expr.hir_id == expr.hir_id;
3128 }
3129 false
3130 }
3131
3132 pub(crate) fn suggest_cast(
3133 &self,
3134 err: &mut Diag<'_>,
3135 expr: &hir::Expr<'_>,
3136 checked_ty: Ty<'tcx>,
3137 expected_ty: Ty<'tcx>,
3138 expected_ty_expr: Option<&'tcx hir::Expr<'tcx>>,
3139 ) -> bool {
3140 if self.tcx.sess.source_map().is_imported(expr.span) {
3141 return false;
3143 }
3144
3145 let span = if let hir::ExprKind::Lit(lit) = &expr.kind { lit.span } else { expr.span };
3146 let Ok(src) = self.tcx.sess.source_map().span_to_snippet(span) else {
3147 return false;
3148 };
3149
3150 let can_cast = false;
3159
3160 let mut sugg = vec![];
3161
3162 if let hir::Node::ExprField(field) = self.tcx.parent_hir_node(expr.hir_id) {
3163 if field.is_shorthand {
3165 sugg.push((field.ident.span.shrink_to_lo(), format!("{}: ", field.ident)));
3167 } else {
3168 return false;
3170 }
3171 };
3172
3173 if let hir::ExprKind::Call(path, args) = &expr.kind
3174 && let (hir::ExprKind::Path(hir::QPath::TypeRelative(base_ty, path_segment)), 1) =
3175 (&path.kind, args.len())
3176 && let (hir::TyKind::Path(hir::QPath::Resolved(None, base_ty_path)), sym::from) =
3178 (&base_ty.kind, path_segment.ident.name)
3179 {
3180 if let Some(ident) = &base_ty_path.segments.iter().map(|s| s.ident).next() {
3181 match ident.name {
3182 sym::i128
3183 | sym::i64
3184 | sym::i32
3185 | sym::i16
3186 | sym::i8
3187 | sym::u128
3188 | sym::u64
3189 | sym::u32
3190 | sym::u16
3191 | sym::u8
3192 | sym::isize
3193 | sym::usize
3194 if base_ty_path.segments.len() == 1 =>
3195 {
3196 return false;
3197 }
3198 _ => {}
3199 }
3200 }
3201 }
3202
3203 let msg = format!(
3204 "you can convert {} `{}` to {} `{}`",
3205 checked_ty.kind().article(),
3206 checked_ty,
3207 expected_ty.kind().article(),
3208 expected_ty,
3209 );
3210 let cast_msg = format!(
3211 "you can cast {} `{}` to {} `{}`",
3212 checked_ty.kind().article(),
3213 checked_ty,
3214 expected_ty.kind().article(),
3215 expected_ty,
3216 );
3217 let lit_msg = format!(
3218 "change the type of the numeric literal from `{checked_ty}` to `{expected_ty}`",
3219 );
3220
3221 let close_paren = if self.precedence(expr) < ExprPrecedence::Unambiguous {
3222 sugg.push((expr.span.shrink_to_lo(), "(".to_string()));
3223 ")"
3224 } else {
3225 ""
3226 };
3227
3228 let mut cast_suggestion = sugg.clone();
3229 cast_suggestion.push((expr.span.shrink_to_hi(), format!("{close_paren} as {expected_ty}")));
3230 let mut into_suggestion = sugg.clone();
3231 into_suggestion.push((expr.span.shrink_to_hi(), format!("{close_paren}.into()")));
3232 let mut suffix_suggestion = sugg.clone();
3233 suffix_suggestion.push((
3234 if matches!(
3235 (expected_ty.kind(), checked_ty.kind()),
3236 (ty::Int(_) | ty::Uint(_), ty::Float(_))
3237 ) {
3238 let src = src.trim_end_matches(&checked_ty.to_string());
3240 let len = src.split('.').next().unwrap().len();
3241 span.with_lo(span.lo() + BytePos(len as u32))
3242 } else {
3243 let len = src.trim_end_matches(&checked_ty.to_string()).len();
3244 span.with_lo(span.lo() + BytePos(len as u32))
3245 },
3246 if self.precedence(expr) < ExprPrecedence::Unambiguous {
3247 format!("{expected_ty})")
3249 } else {
3250 expected_ty.to_string()
3251 },
3252 ));
3253 let literal_is_ty_suffixed = |expr: &hir::Expr<'_>| {
3254 if let hir::ExprKind::Lit(lit) = &expr.kind { lit.node.is_suffixed() } else { false }
3255 };
3256 let is_negative_int =
3257 |expr: &hir::Expr<'_>| matches!(expr.kind, hir::ExprKind::Unary(hir::UnOp::Neg, ..));
3258 let is_uint = |ty: Ty<'_>| matches!(ty.kind(), ty::Uint(..));
3259
3260 let in_const_context = self.tcx.hir_is_inside_const_context(expr.hir_id);
3261
3262 let suggest_fallible_into_or_lhs_from =
3263 |err: &mut Diag<'_>, exp_to_found_is_fallible: bool| {
3264 let lhs_expr_and_src = expected_ty_expr.and_then(|expr| {
3271 self.tcx
3272 .sess
3273 .source_map()
3274 .span_to_snippet(expr.span)
3275 .ok()
3276 .map(|src| (expr, src))
3277 });
3278 let (msg, suggestion) = if let (Some((lhs_expr, lhs_src)), false) =
3279 (lhs_expr_and_src, exp_to_found_is_fallible)
3280 {
3281 let msg = format!(
3282 "you can convert `{lhs_src}` from `{expected_ty}` to `{checked_ty}`, matching the type of `{src}`",
3283 );
3284 let suggestion = vec![
3285 (lhs_expr.span.shrink_to_lo(), format!("{checked_ty}::from(")),
3286 (lhs_expr.span.shrink_to_hi(), ")".to_string()),
3287 ];
3288 (msg, suggestion)
3289 } else {
3290 let msg =
3291 format!("{} and panic if the converted value doesn't fit", msg.clone());
3292 let mut suggestion = sugg.clone();
3293 suggestion.push((
3294 expr.span.shrink_to_hi(),
3295 format!("{close_paren}.try_into().unwrap()"),
3296 ));
3297 (msg, suggestion)
3298 };
3299 err.multipart_suggestion_verbose(msg, suggestion, Applicability::MachineApplicable);
3300 };
3301
3302 let suggest_to_change_suffix_or_into =
3303 |err: &mut Diag<'_>, found_to_exp_is_fallible: bool, exp_to_found_is_fallible: bool| {
3304 let exp_is_lhs = expected_ty_expr.is_some_and(|e| self.tcx.hir_is_lhs(e.hir_id));
3305
3306 if exp_is_lhs {
3307 return;
3308 }
3309
3310 let always_fallible = found_to_exp_is_fallible
3311 && (exp_to_found_is_fallible || expected_ty_expr.is_none());
3312 let msg = if literal_is_ty_suffixed(expr) {
3313 lit_msg.clone()
3314 } else if always_fallible && (is_negative_int(expr) && is_uint(expected_ty)) {
3315 let msg = format!("`{src}` cannot fit into type `{expected_ty}`");
3319 err.note(msg);
3320 return;
3321 } else if in_const_context {
3322 return;
3324 } else if found_to_exp_is_fallible {
3325 return suggest_fallible_into_or_lhs_from(err, exp_to_found_is_fallible);
3326 } else {
3327 msg.clone()
3328 };
3329 let suggestion = if literal_is_ty_suffixed(expr) {
3330 suffix_suggestion.clone()
3331 } else {
3332 into_suggestion.clone()
3333 };
3334 err.multipart_suggestion_verbose(msg, suggestion, Applicability::MachineApplicable);
3335 };
3336
3337 match (expected_ty.kind(), checked_ty.kind()) {
3338 (ty::Int(exp), ty::Int(found)) => {
3339 let (f2e_is_fallible, e2f_is_fallible) = match (exp.bit_width(), found.bit_width())
3340 {
3341 (Some(exp), Some(found)) if exp < found => (true, false),
3342 (Some(exp), Some(found)) if exp > found => (false, true),
3343 (None, Some(8 | 16)) => (false, true),
3344 (Some(8 | 16), None) => (true, false),
3345 (None, _) | (_, None) => (true, true),
3346 _ => (false, false),
3347 };
3348 suggest_to_change_suffix_or_into(err, f2e_is_fallible, e2f_is_fallible);
3349 true
3350 }
3351 (ty::Uint(exp), ty::Uint(found)) => {
3352 let (f2e_is_fallible, e2f_is_fallible) = match (exp.bit_width(), found.bit_width())
3353 {
3354 (Some(exp), Some(found)) if exp < found => (true, false),
3355 (Some(exp), Some(found)) if exp > found => (false, true),
3356 (None, Some(8 | 16)) => (false, true),
3357 (Some(8 | 16), None) => (true, false),
3358 (None, _) | (_, None) => (true, true),
3359 _ => (false, false),
3360 };
3361 suggest_to_change_suffix_or_into(err, f2e_is_fallible, e2f_is_fallible);
3362 true
3363 }
3364 (&ty::Int(exp), &ty::Uint(found)) => {
3365 let (f2e_is_fallible, e2f_is_fallible) = match (exp.bit_width(), found.bit_width())
3366 {
3367 (Some(exp), Some(found)) if found < exp => (false, true),
3368 (None, Some(8)) => (false, true),
3369 _ => (true, true),
3370 };
3371 suggest_to_change_suffix_or_into(err, f2e_is_fallible, e2f_is_fallible);
3372 true
3373 }
3374 (&ty::Uint(exp), &ty::Int(found)) => {
3375 let (f2e_is_fallible, e2f_is_fallible) = match (exp.bit_width(), found.bit_width())
3376 {
3377 (Some(exp), Some(found)) if found > exp => (true, false),
3378 (Some(8), None) => (true, false),
3379 _ => (true, true),
3380 };
3381 suggest_to_change_suffix_or_into(err, f2e_is_fallible, e2f_is_fallible);
3382 true
3383 }
3384 (ty::Float(exp), ty::Float(found)) => {
3385 if found.bit_width() < exp.bit_width() {
3386 suggest_to_change_suffix_or_into(err, false, true);
3387 } else if literal_is_ty_suffixed(expr) {
3388 err.multipart_suggestion_verbose(
3389 lit_msg,
3390 suffix_suggestion,
3391 Applicability::MachineApplicable,
3392 );
3393 } else if can_cast {
3394 err.multipart_suggestion_verbose(
3396 format!("{cast_msg}, producing the closest possible value"),
3397 cast_suggestion,
3398 Applicability::MaybeIncorrect, );
3400 }
3401 true
3402 }
3403 (&ty::Uint(_) | &ty::Int(_), &ty::Float(_)) => {
3404 if literal_is_ty_suffixed(expr) {
3405 err.multipart_suggestion_verbose(
3406 lit_msg,
3407 suffix_suggestion,
3408 Applicability::MachineApplicable,
3409 );
3410 } else if can_cast {
3411 err.multipart_suggestion_verbose(
3413 format!("{msg}, rounding the float towards zero"),
3414 cast_suggestion,
3415 Applicability::MaybeIncorrect, );
3417 }
3418 true
3419 }
3420 (ty::Float(exp), ty::Uint(found)) => {
3421 if exp.bit_width() > found.bit_width().unwrap_or(256) {
3423 err.multipart_suggestion_verbose(
3424 format!(
3425 "{msg}, producing the floating point representation of the integer",
3426 ),
3427 into_suggestion,
3428 Applicability::MachineApplicable,
3429 );
3430 } else if literal_is_ty_suffixed(expr) {
3431 err.multipart_suggestion_verbose(
3432 lit_msg,
3433 suffix_suggestion,
3434 Applicability::MachineApplicable,
3435 );
3436 } else {
3437 err.multipart_suggestion_verbose(
3439 format!(
3440 "{cast_msg}, producing the floating point representation of the integer, \
3441 rounded if necessary",
3442 ),
3443 cast_suggestion,
3444 Applicability::MaybeIncorrect, );
3446 }
3447 true
3448 }
3449 (ty::Float(exp), ty::Int(found)) => {
3450 if exp.bit_width() > found.bit_width().unwrap_or(256) {
3452 err.multipart_suggestion_verbose(
3453 format!(
3454 "{}, producing the floating point representation of the integer",
3455 msg.clone(),
3456 ),
3457 into_suggestion,
3458 Applicability::MachineApplicable,
3459 );
3460 } else if literal_is_ty_suffixed(expr) {
3461 err.multipart_suggestion_verbose(
3462 lit_msg,
3463 suffix_suggestion,
3464 Applicability::MachineApplicable,
3465 );
3466 } else {
3467 err.multipart_suggestion_verbose(
3469 format!(
3470 "{}, producing the floating point representation of the integer, \
3471 rounded if necessary",
3472 &msg,
3473 ),
3474 cast_suggestion,
3475 Applicability::MaybeIncorrect, );
3477 }
3478 true
3479 }
3480 (
3481 &ty::Uint(ty::UintTy::U32 | ty::UintTy::U64 | ty::UintTy::U128)
3482 | &ty::Int(ty::IntTy::I32 | ty::IntTy::I64 | ty::IntTy::I128),
3483 &ty::Char,
3484 ) => {
3485 err.multipart_suggestion_verbose(
3486 format!("{cast_msg}, since a `char` always occupies 4 bytes"),
3487 cast_suggestion,
3488 Applicability::MachineApplicable,
3489 );
3490 true
3491 }
3492 _ => false,
3493 }
3494 }
3495
3496 pub(crate) fn suggest_method_call_on_range_literal(
3498 &self,
3499 err: &mut Diag<'_>,
3500 expr: &hir::Expr<'tcx>,
3501 checked_ty: Ty<'tcx>,
3502 expected_ty: Ty<'tcx>,
3503 ) {
3504 if !hir::is_range_literal(expr) {
3505 return;
3506 }
3507 let hir::ExprKind::Struct(&qpath, [start, end], _) = expr.kind else {
3508 return;
3509 };
3510 if !self.tcx.qpath_is_lang_item(qpath, LangItem::Range) {
3511 return;
3512 }
3513 if let hir::Node::ExprField(_) = self.tcx.parent_hir_node(expr.hir_id) {
3514 return;
3516 }
3517 let mut expr = end.expr;
3518 let mut expectation = Some(expected_ty);
3519 while let hir::ExprKind::MethodCall(_, rcvr, ..) = expr.kind {
3520 expr = rcvr;
3523 expectation = None;
3526 }
3527 let hir::ExprKind::Call(method_name, _) = expr.kind else {
3528 return;
3529 };
3530 let ty::Adt(adt, _) = checked_ty.kind() else {
3531 return;
3532 };
3533 if self.tcx.lang_items().range_struct() != Some(adt.did()) {
3534 return;
3535 }
3536 if let ty::Adt(adt, _) = expected_ty.kind()
3537 && self.tcx.is_lang_item(adt.did(), LangItem::Range)
3538 {
3539 return;
3540 }
3541 let hir::ExprKind::Path(hir::QPath::Resolved(None, p)) = method_name.kind else {
3543 return;
3544 };
3545 let [hir::PathSegment { ident, .. }] = p.segments else {
3546 return;
3547 };
3548 let self_ty = self.typeck_results.borrow().expr_ty(start.expr);
3549 let Ok(_pick) = self.lookup_probe_for_diagnostic(
3550 *ident,
3551 self_ty,
3552 expr,
3553 probe::ProbeScope::AllTraits,
3554 expectation,
3555 ) else {
3556 return;
3557 };
3558 let mut sugg = ".";
3559 let mut span = start.expr.span.between(end.expr.span);
3560 if span.lo() + BytePos(2) == span.hi() {
3561 span = span.with_lo(span.lo() + BytePos(1));
3564 sugg = "";
3565 }
3566 err.span_suggestion_verbose(
3567 span,
3568 "you likely meant to write a method call instead of a range",
3569 sugg,
3570 Applicability::MachineApplicable,
3571 );
3572 }
3573
3574 pub(crate) fn suggest_return_binding_for_missing_tail_expr(
3577 &self,
3578 err: &mut Diag<'_>,
3579 expr: &hir::Expr<'_>,
3580 checked_ty: Ty<'tcx>,
3581 expected_ty: Ty<'tcx>,
3582 ) {
3583 if !checked_ty.is_unit() {
3584 return;
3585 }
3586 let hir::ExprKind::Path(hir::QPath::Resolved(None, path)) = expr.kind else {
3587 return;
3588 };
3589 let hir::def::Res::Local(hir_id) = path.res else {
3590 return;
3591 };
3592 let hir::Node::Pat(pat) = self.tcx.hir_node(hir_id) else {
3593 return;
3594 };
3595 let hir::Node::LetStmt(hir::LetStmt { ty: None, init: Some(init), .. }) =
3596 self.tcx.parent_hir_node(pat.hir_id)
3597 else {
3598 return;
3599 };
3600 let hir::ExprKind::Block(block, None) = init.kind else {
3601 return;
3602 };
3603 if block.expr.is_some() {
3604 return;
3605 }
3606 let [.., stmt] = block.stmts else {
3607 err.span_label(block.span, "this empty block is missing a tail expression");
3608 return;
3609 };
3610 let hir::StmtKind::Semi(tail_expr) = stmt.kind else {
3611 return;
3612 };
3613 let Some(ty) = self.node_ty_opt(tail_expr.hir_id) else {
3614 return;
3615 };
3616 if self.can_eq(self.param_env, expected_ty, ty)
3617 && stmt.span.hi() != tail_expr.span.hi()
3622 {
3623 err.span_suggestion_short(
3624 stmt.span.with_lo(tail_expr.span.hi()),
3625 "remove this semicolon",
3626 "",
3627 Applicability::MachineApplicable,
3628 );
3629 } else {
3630 err.span_label(block.span, "this block is missing a tail expression");
3631 }
3632 }
3633
3634 pub(crate) fn suggest_swapping_lhs_and_rhs(
3635 &self,
3636 err: &mut Diag<'_>,
3637 rhs_ty: Ty<'tcx>,
3638 lhs_ty: Ty<'tcx>,
3639 rhs_expr: &'tcx hir::Expr<'tcx>,
3640 lhs_expr: &'tcx hir::Expr<'tcx>,
3641 ) {
3642 if let Some(partial_eq_def_id) = self.infcx.tcx.lang_items().eq_trait()
3643 && self
3644 .infcx
3645 .type_implements_trait(partial_eq_def_id, [rhs_ty, lhs_ty], self.param_env)
3646 .must_apply_modulo_regions()
3647 {
3648 let sm = self.tcx.sess.source_map();
3649 if !rhs_expr.span.in_external_macro(sm)
3652 && !lhs_expr.span.in_external_macro(sm)
3653 && let Ok(rhs_snippet) = sm.span_to_snippet(rhs_expr.span)
3654 && let Ok(lhs_snippet) = sm.span_to_snippet(lhs_expr.span)
3655 {
3656 err.note(format!("`{rhs_ty}` implements `PartialEq<{lhs_ty}>`"));
3657 err.multipart_suggestion(
3658 "consider swapping the equality",
3659 vec![(lhs_expr.span, rhs_snippet), (rhs_expr.span, lhs_snippet)],
3660 Applicability::MaybeIncorrect,
3661 );
3662 }
3663 }
3664 }
3665}