Skip to content

Commit

Permalink
errors: span_suggestion takes impl ToString
Browse files Browse the repository at this point in the history
Change `span_suggestion` (and variants) to take `impl ToString` rather
than `String` for the suggested code, as this simplifies the
requirements on the diagnostic derive.

Signed-off-by: David Wood <[email protected]>
  • Loading branch information
davidtwco committed Apr 29, 2022
1 parent baaa3b6 commit 73fa217
Show file tree
Hide file tree
Showing 17 changed files with 40 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
err.span_suggestion_verbose(
span,
"consider changing this to be mutable",
" mut ".into(),
" mut ",
Applicability::MaybeIncorrect,
);
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_builtin_macros/src/deriving/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ fn validate_default_attribute(
.span_suggestion_hidden(
attr.span,
"try using `#[default]`",
"#[default]".into(),
"#[default]",
Applicability::MaybeIncorrect,
)
.emit();
Expand Down
14 changes: 7 additions & 7 deletions compiler/rustc_errors/src/diagnostic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,7 @@ impl Diagnostic {
&mut self,
sp: Span,
msg: impl Into<DiagnosticMessage>,
suggestion: String,
suggestion: impl ToString,
applicability: Applicability,
) -> &mut Self {
self.span_suggestion_with_style(
Expand All @@ -623,13 +623,13 @@ impl Diagnostic {
&mut self,
sp: Span,
msg: impl Into<DiagnosticMessage>,
suggestion: String,
suggestion: impl ToString,
applicability: Applicability,
style: SuggestionStyle,
) -> &mut Self {
self.push_suggestion(CodeSuggestion {
substitutions: vec![Substitution {
parts: vec![SubstitutionPart { snippet: suggestion, span: sp }],
parts: vec![SubstitutionPart { snippet: suggestion.to_string(), span: sp }],
}],
msg: msg.into(),
style,
Expand All @@ -643,7 +643,7 @@ impl Diagnostic {
&mut self,
sp: Span,
msg: impl Into<DiagnosticMessage>,
suggestion: String,
suggestion: impl ToString,
applicability: Applicability,
) -> &mut Self {
self.span_suggestion_with_style(
Expand Down Expand Up @@ -711,7 +711,7 @@ impl Diagnostic {
&mut self,
sp: Span,
msg: impl Into<DiagnosticMessage>,
suggestion: String,
suggestion: impl ToString,
applicability: Applicability,
) -> &mut Self {
self.span_suggestion_with_style(
Expand All @@ -734,7 +734,7 @@ impl Diagnostic {
&mut self,
sp: Span,
msg: impl Into<DiagnosticMessage>,
suggestion: String,
suggestion: impl ToString,
applicability: Applicability,
) -> &mut Self {
self.span_suggestion_with_style(
Expand All @@ -755,7 +755,7 @@ impl Diagnostic {
&mut self,
sp: Span,
msg: impl Into<DiagnosticMessage>,
suggestion: String,
suggestion: impl ToString,
applicability: Applicability,
) -> &mut Self {
self.span_suggestion_with_style(
Expand Down
10 changes: 5 additions & 5 deletions compiler/rustc_errors/src/diagnostic_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ impl<'a, G: EmissionGuarantee> DiagnosticBuilder<'a, G> {
&mut self,
sp: Span,
msg: impl Into<DiagnosticMessage>,
suggestion: String,
suggestion: impl ToString,
applicability: Applicability,
) -> &mut Self);
forward!(pub fn span_suggestions(
Expand All @@ -497,28 +497,28 @@ impl<'a, G: EmissionGuarantee> DiagnosticBuilder<'a, G> {
&mut self,
sp: Span,
msg: impl Into<DiagnosticMessage>,
suggestion: String,
suggestion: impl ToString,
applicability: Applicability,
) -> &mut Self);
forward!(pub fn span_suggestion_verbose(
&mut self,
sp: Span,
msg: impl Into<DiagnosticMessage>,
suggestion: String,
suggestion: impl ToString,
applicability: Applicability,
) -> &mut Self);
forward!(pub fn span_suggestion_hidden(
&mut self,
sp: Span,
msg: impl Into<DiagnosticMessage>,
suggestion: String,
suggestion: impl ToString,
applicability: Applicability,
) -> &mut Self);
forward!(pub fn tool_only_span_suggestion(
&mut self,
sp: Span,
msg: impl Into<DiagnosticMessage>,
suggestion: String,
suggestion: impl ToString,
applicability: Applicability,
) -> &mut Self);

Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_expand/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@ pub fn parse_cfg<'a>(meta_item: &'a MetaItem, sess: &Session) -> Option<&'a Meta
err.span_suggestion(
span,
"expected syntax is",
suggestion.into(),
suggestion,
Applicability::HasPlaceholders,
);
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_infer/src/infer/error_reporting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2198,7 +2198,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
err.span_suggestion(
span.with_hi(before_close).shrink_to_hi(),
msg,
",".into(),
",",
Applicability::MachineApplicable,
);
} else {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_lint/src/array_into_iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ impl<'tcx> LateLintPass<'tcx> for ArrayIntoIter {
diag.span_suggestion(
call.ident.span,
"use `.iter()` instead of `.into_iter()` to avoid ambiguity",
"iter".into(),
"iter",
Applicability::MachineApplicable,
);
if self.for_expr_span == expr.span {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_lint/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -738,7 +738,7 @@ pub trait LintContext: Sized {
db.span_suggestion_verbose(
span.shrink_to_hi(),
"insert whitespace here to avoid this being parsed as a prefix in Rust 2021",
" ".into(),
" ",
Applicability::MachineApplicable,
);
}
Expand Down
10 changes: 5 additions & 5 deletions compiler/rustc_lint/src/non_fmt_panic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ fn check_panic<'tcx>(cx: &LateContext<'tcx>, f: &'tcx hir::Expr<'tcx>, arg: &'tc
l.span_suggestion_verbose(
arg_span.shrink_to_lo(),
"add a \"{}\" format string to Display the message",
"\"{}\", ".into(),
"\"{}\", ",
fmt_applicability,
);
} else if suggest_debug {
Expand All @@ -186,7 +186,7 @@ fn check_panic<'tcx>(cx: &LateContext<'tcx>, f: &'tcx hir::Expr<'tcx>, arg: &'tc
"add a \"{{:?}}\" format string to use the Debug implementation of `{}`",
ty,
),
"\"{:?}\", ".into(),
"\"{:?}\", ",
fmt_applicability,
);
}
Expand Down Expand Up @@ -266,13 +266,13 @@ fn check_panic_str<'tcx>(
l.span_suggestion(
arg.span.shrink_to_hi(),
&format!("add the missing argument{}", pluralize!(n_arguments)),
", ...".into(),
", ...",
Applicability::HasPlaceholders,
);
l.span_suggestion(
arg.span.shrink_to_lo(),
"or add a \"{}\" format string to use the message literally",
"\"{}\", ".into(),
"\"{}\", ",
Applicability::MachineApplicable,
);
}
Expand All @@ -297,7 +297,7 @@ fn check_panic_str<'tcx>(
l.span_suggestion(
arg.span.shrink_to_lo(),
"add a \"{}\" format string to use the message literally",
"\"{}\", ".into(),
"\"{}\", ",
Applicability::MachineApplicable,
);
}
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_parse/src/lexer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -612,14 +612,14 @@ impl<'a> StringReader<'a> {
err.span_suggestion_verbose(
prefix_span,
"use `br` for a raw byte string",
"br".to_string(),
"br",
Applicability::MaybeIncorrect,
);
} else if expn_data.is_root() {
err.span_suggestion_verbose(
prefix_span.shrink_to_hi(),
"consider inserting whitespace here",
" ".into(),
" ",
Applicability::MaybeIncorrect,
);
}
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_parse/src/parser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1771,7 +1771,7 @@ impl<'a> Parser<'a> {
.span_suggestion(
token.span,
"must have an integer part",
pprust::token_to_string(token).into(),
pprust::token_to_string(token),
Applicability::MachineApplicable,
)
.emit();
Expand Down Expand Up @@ -2324,7 +2324,7 @@ impl<'a> Parser<'a> {
.span_suggestion_short(
span,
msg,
sugg.into(),
sugg,
// Has been misleading, at least in the past (closed Issue #48492).
Applicability::MaybeIncorrect,
)
Expand Down Expand Up @@ -2828,7 +2828,7 @@ impl<'a> Parser<'a> {
e.span_suggestion(
self.prev_token.span.shrink_to_hi(),
"try adding a comma",
",".into(),
",",
Applicability::MachineApplicable,
);
}
Expand Down
10 changes: 5 additions & 5 deletions compiler/rustc_parse/src/parser/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ impl<'a> Parser<'a> {
err.span_suggestion_short(
sp,
&msg,
" struct ".into(),
" struct ",
Applicability::MaybeIncorrect, // speculative
);
Err(err)
Expand Down Expand Up @@ -532,13 +532,13 @@ impl<'a> Parser<'a> {
.span_suggestion(
span,
"add a trait here",
" Trait ".into(),
" Trait ",
Applicability::HasPlaceholders,
)
.span_suggestion(
span.to(self.token.span),
"for an inherent impl, drop this `for`",
"".into(),
"",
Applicability::MaybeIncorrect,
)
.emit();
Expand Down Expand Up @@ -1459,7 +1459,7 @@ impl<'a> Parser<'a> {
err.span_suggestion(
sp,
"missing comma here",
",".into(),
",",
Applicability::MachineApplicable,
);
}
Expand Down Expand Up @@ -1498,7 +1498,7 @@ impl<'a> Parser<'a> {
err.span_suggestion(
sp,
"try adding a comma",
",".into(),
",",
Applicability::MachineApplicable,
);
err.emit();
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_parse/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -802,7 +802,7 @@ impl<'a> Parser<'a> {
.span_suggestion_verbose(
self.prev_token.span.shrink_to_hi().until(self.token.span),
&msg,
" @ ".to_string(),
" @ ",
Applicability::MaybeIncorrect,
)
.emit();
Expand All @@ -818,7 +818,7 @@ impl<'a> Parser<'a> {
.span_suggestion_short(
sp,
&format!("missing `{}`", token_str),
token_str.into(),
token_str,
Applicability::MaybeIncorrect,
)
.emit();
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_parse/src/parser/pat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -606,7 +606,7 @@ impl<'a> Parser<'a> {
.span_suggestion(
mutref_span,
"try switching the order",
"ref mut".into(),
"ref mut",
Applicability::MachineApplicable,
)
.emit();
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_resolve/src/build_reduced_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -851,7 +851,7 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
.span_suggestion(
item.span,
"rename the `self` crate to be able to import it",
"extern crate self as name;".into(),
"extern crate self as name;",
Applicability::HasPlaceholders,
)
.emit();
Expand Down
2 changes: 1 addition & 1 deletion src/tools/clippy/clippy_lints/src/functions/must_use.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ fn check_needless_must_use(
diag.span_suggestion(
attr.span,
"remove the attribute",
"".into(),
"",
Applicability::MachineApplicable,
);
},
Expand Down
4 changes: 2 additions & 2 deletions src/tools/clippy/clippy_lints/src/needless_pass_by_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessPassByValue {
|x| Cow::from(format!("change `{}` to", x)),
)
.as_ref(),
suggestion.into(),
suggestion,
Applicability::Unspecified,
);
}
Expand Down Expand Up @@ -271,7 +271,7 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessPassByValue {
|x| Cow::from(format!("change `{}` to", x))
)
.as_ref(),
suggestion.into(),
suggestion,
Applicability::Unspecified,
);
}
Expand Down

0 comments on commit 73fa217

Please sign in to comment.