diff --git a/compiler/rustc_attr_parsing/src/parser.rs b/compiler/rustc_attr_parsing/src/parser.rs index 4d714fccc3f61..bd256d98ecd82 100644 --- a/compiler/rustc_attr_parsing/src/parser.rs +++ b/compiler/rustc_attr_parsing/src/parser.rs @@ -476,12 +476,14 @@ fn expr_to_lit<'sess>( // Suggest adding quotation marks to turn an identifier into a string literal if let ExprKind::Path(None, ref path) = expr.kind - && let [segment] = path.segments.as_slice() + && let [_] = path.segments.as_slice() { - err.span_suggestion( - expr.span, - "try adding quotation marks", - &format!("\"{}\"", segment.ident), + err.multipart_suggestion( + "you might have meant to write a string literal", + vec![ + (expr.span.shrink_to_lo(), "\"".to_string()), + (expr.span.shrink_to_hi(), "\"".to_string()), + ], Applicability::MaybeIncorrect, ); } diff --git a/tests/ui/attributes/crate-type-non-crate.stderr b/tests/ui/attributes/crate-type-non-crate.stderr index d27d81f784254..8edfebdd918df 100644 --- a/tests/ui/attributes/crate-type-non-crate.stderr +++ b/tests/ui/attributes/crate-type-non-crate.stderr @@ -40,7 +40,12 @@ error: attribute value must be a literal --> $DIR/crate-type-non-crate.rs:9:16 | LL | #[crate_type = lib] - | ^^^ help: try adding quotation marks: `"lib"` + | ^^^ + | +help: you might have meant to write a string literal + | +LL | #[crate_type = "lib"] + | + + error[E0539]: malformed `crate_type` attribute input --> $DIR/crate-type-non-crate.rs:12:1 @@ -72,7 +77,12 @@ error: attribute value must be a literal --> $DIR/crate-type-non-crate.rs:14:16 | LL | #[crate_type = foo] - | ^^^ help: try adding quotation marks: `"foo"` + | ^^^ + | +help: you might have meant to write a string literal + | +LL | #[crate_type = "foo"] + | + + error[E0539]: malformed `crate_type` attribute input --> $DIR/crate-type-non-crate.rs:17:1 diff --git a/tests/ui/attributes/validation-on-associated-items-issue-121537.stderr b/tests/ui/attributes/validation-on-associated-items-issue-121537.stderr index 750ca2e3aaca2..94a77253ae409 100644 --- a/tests/ui/attributes/validation-on-associated-items-issue-121537.stderr +++ b/tests/ui/attributes/validation-on-associated-items-issue-121537.stderr @@ -2,7 +2,12 @@ error: attribute value must be a literal --> $DIR/validation-on-associated-items-issue-121537.rs:2:13 | LL | #[doc = MyTrait] - | ^^^^^^^ help: try adding quotation marks: `"MyTrait"` + | ^^^^^^^ + | +help: you might have meant to write a string literal + | +LL | #[doc = "MyTrait"] + | + + error: aborting due to 1 previous error