Skip to content

Commit d256463

Browse files
Rollup merge of #156797 - ariagivens:suggest-quotes, r=JonathanBrouwer
Suggest adding quotation marks to identifiers in attributes When the user provides an identifier when a literal was expected in an attribute value, suggest adding quotation marks to the identifier to make it a string literal. For instance: ``` 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"` ```
2 parents 625a0c2 + 8fec623 commit d256463

3 files changed

Lines changed: 18 additions & 5 deletions

File tree

compiler/rustc_attr_parsing/src/parser.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use rustc_ast::{
1212
AttrArgs, Expr, ExprKind, LitKind, MetaItemLit, Path, PathSegment, StmtKind, UnOp,
1313
};
1414
use rustc_ast_pretty::pprust;
15-
use rustc_errors::{Diag, PResult};
15+
use rustc_errors::{Applicability, Diag, PResult};
1616
use rustc_hir::{self as hir, AttrPath};
1717
use rustc_parse::exp;
1818
use rustc_parse::parser::{ForceCollect, Parser, PathStyle, Recovery, token_descr};
@@ -410,7 +410,20 @@ fn expr_to_lit<'sess>(
410410
// - `#[foo = include_str!("nonexistent-file.rs")]`:
411411
// results in `ast::ExprKind::Err`.
412412
let msg = "attribute value must be a literal";
413-
let err = psess.dcx().struct_span_err(span, msg);
413+
let mut err = psess.dcx().struct_span_err(span, msg);
414+
415+
// Suggest adding quotation marks to turn an identifier into a string literal
416+
if let ExprKind::Path(None, ref path) = expr.kind
417+
&& let [segment] = path.segments.as_slice()
418+
{
419+
err.span_suggestion(
420+
expr.span,
421+
"try adding quotation marks",
422+
&format!("\"{}\"", segment.ident),
423+
Applicability::MaybeIncorrect,
424+
);
425+
}
426+
414427
Err(err)
415428
}
416429
}

tests/ui/attributes/crate-type-non-crate.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ error: attribute value must be a literal
4040
--> $DIR/crate-type-non-crate.rs:9:16
4141
|
4242
LL | #[crate_type = lib]
43-
| ^^^
43+
| ^^^ help: try adding quotation marks: `"lib"`
4444

4545
error[E0539]: malformed `crate_type` attribute input
4646
--> $DIR/crate-type-non-crate.rs:12:1
@@ -72,7 +72,7 @@ error: attribute value must be a literal
7272
--> $DIR/crate-type-non-crate.rs:14:16
7373
|
7474
LL | #[crate_type = foo]
75-
| ^^^
75+
| ^^^ help: try adding quotation marks: `"foo"`
7676

7777
error[E0539]: malformed `crate_type` attribute input
7878
--> $DIR/crate-type-non-crate.rs:17:1

tests/ui/attributes/validation-on-associated-items-issue-121537.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error: attribute value must be a literal
22
--> $DIR/validation-on-associated-items-issue-121537.rs:2:13
33
|
44
LL | #[doc = MyTrait]
5-
| ^^^^^^^
5+
| ^^^^^^^ help: try adding quotation marks: `"MyTrait"`
66

77
error: aborting due to 1 previous error
88

0 commit comments

Comments
 (0)