about summary refs log tree commit diff
path: root/compiler/rustc_resolve/src
diff options
context:
space:
mode:
authorxizheyin <xizheyin@smail.nju.edu.cn>2025-05-18 16:14:48 +0800
committerxizheyin <xizheyin@smail.nju.edu.cn>2025-05-18 16:14:48 +0800
commit9de7fff0d8ab72fb57dea6255fc10fe35219db72 (patch)
tree6de558e92852753c10ce9256d7c196f855144b28 /compiler/rustc_resolve/src
parentfe0663c33d24c903812ec0fe58b94d68aa275b98 (diff)
downloadrust-9de7fff0d8ab72fb57dea6255fc10fe35219db72.tar.gz
rust-9de7fff0d8ab72fb57dea6255fc10fe35219db72.zip
Suggest use `"{}", self.x` instead of `{self.x}` when resolve `x` as field of `self`
Signed-off-by: xizheyin <xizheyin@smail.nju.edu.cn>
Diffstat (limited to 'compiler/rustc_resolve/src')
-rw-r--r--compiler/rustc_resolve/src/late/diagnostics.rs30
1 files changed, 24 insertions, 6 deletions
diff --git a/compiler/rustc_resolve/src/late/diagnostics.rs b/compiler/rustc_resolve/src/late/diagnostics.rs
index b538be34f31..30c2125d0b6 100644
--- a/compiler/rustc_resolve/src/late/diagnostics.rs
+++ b/compiler/rustc_resolve/src/late/diagnostics.rs
@@ -751,12 +751,30 @@ impl<'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
                 match candidate {
                     AssocSuggestion::Field(field_span) => {
                         if self_is_available {
-                            err.span_suggestion_verbose(
-                                span.shrink_to_lo(),
-                                "you might have meant to use the available field",
-                                format!("{pre}self."),
-                                Applicability::MachineApplicable,
-                            );
+                            let source_map = self.r.tcx.sess.source_map();
+                            // check if the field is used in a format string, such as `"{x}"`
+                            let field_is_format_named_arg =
+                                source_map.span_to_source(span, |s, start, _| {
+                                    if let Some(expanded_expr) = s.get(start - 1..start)
+                                        && expanded_expr.starts_with("{")
+                                    {
+                                        Ok(true)
+                                    } else {
+                                        Ok(false)
+                                    }
+                                });
+                            if let Ok(true) = field_is_format_named_arg {
+                                err.help(
+                                    format!("you might have meant to use the available field in a format string: `\"{{}}\", self.{}`", segment.ident.name),
+                                );
+                            } else {
+                                err.span_suggestion_verbose(
+                                    span.shrink_to_lo(),
+                                    "you might have meant to use the available field",
+                                    format!("{pre}self."),
+                                    Applicability::MachineApplicable,
+                                );
+                            }
                         } else {
                             err.span_label(field_span, "a field by that name exists in `Self`");
                         }