Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Add lint for String in diagnostic implementation #108461

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions compiler/rustc_lint/locales/en-US.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ lint_non_existant_doc_keyword = found non-existing keyword `{$keyword}` used in
lint_diag_out_of_impl =
diagnostics should only be created in `IntoDiagnostic`/`AddToDiagnostic` impls

lint_string_in_diag = use of String in diagnostic
.note = this could indicate incorrectly eagerly converting to a string

lint_untranslatable_diag = diagnostics should be created using translatable messages

lint_bad_opt_access = {$msg}
Expand Down
34 changes: 32 additions & 2 deletions compiler/rustc_lint/src/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use crate::lints::{
BadOptAccessDiag, DefaultHashTypesDiag, DiagOutOfImpl, LintPassByHand, NonExistantDocKeyword,
QueryInstability, TyQualified, TykindDiag, TykindKind, UntranslatableDiag,
QueryInstability, StringInDiagnostic, TyQualified, TykindDiag, TykindKind, UntranslatableDiag,
};
use crate::{EarlyContext, EarlyLintPass, LateContext, LateLintPass, LintContext};
use rustc_ast as ast;
Expand Down Expand Up @@ -338,7 +338,14 @@ declare_tool_lint! {
report_in_external_macro: true
}

declare_lint_pass!(Diagnostics => [ UNTRANSLATABLE_DIAGNOSTIC, DIAGNOSTIC_OUTSIDE_OF_IMPL ]);
declare_tool_lint! {
pub rustc::STRING_IN_DIAGNOSTIC,
Warn,
"prevent the use of `String` in diagnostics, which could indicate incorrect eager conversion",
report_in_external_macro: true
}

declare_lint_pass!(Diagnostics => [ UNTRANSLATABLE_DIAGNOSTIC, DIAGNOSTIC_OUTSIDE_OF_IMPL, STRING_IN_DIAGNOSTIC ]);

impl LateLintPass<'_> for Diagnostics {
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) {
Expand Down Expand Up @@ -395,6 +402,29 @@ impl LateLintPass<'_> for Diagnostics {
}
}

impl EarlyLintPass for Diagnostics {
fn check_item(&mut self, cx: &EarlyContext<'_>, item: &ast::Item) {
if cx.sess().find_by_name(&item.attrs, sym::diag).is_none() {
return;
}

let variants = match &item.kind {
ast::ItemKind::Struct(variant, _) => vec![variant],
ast::ItemKind::Enum(enum_def, _) => enum_def.variants.iter().map(|v| &v.data).collect(),
_ => vec![],
};

for field in variants.iter().flat_map(|d| d.fields()) {
if let ast::TyKind::Path(_, path) = &field.ty.kind
&& let [path] = path.segments.as_ref()
&& path.ident.name == sym::String
{
cx.emit_spanned_lint(STRING_IN_DIAGNOSTIC, path.span(), StringInDiagnostic);
}
}
}
}

declare_tool_lint! {
pub rustc::BAD_OPT_ACCESS,
Deny,
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_lint/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,7 @@ fn register_internals(store: &mut LintStore) {
store.register_lints(&TyTyKind::get_lints());
store.register_late_pass(|_| Box::new(TyTyKind));
store.register_lints(&Diagnostics::get_lints());
store.register_early_pass(|| Box::new(Diagnostics));
store.register_late_pass(|_| Box::new(Diagnostics));
store.register_lints(&BadOptAccess::get_lints());
store.register_late_pass(|_| Box::new(BadOptAccess));
Expand Down
5 changes: 5 additions & 0 deletions compiler/rustc_lint/src/lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -800,6 +800,11 @@ pub struct NonExistantDocKeyword {
#[diag(lint_diag_out_of_impl)]
pub struct DiagOutOfImpl;

#[derive(LintDiagnostic)]
#[diag(lint_string_in_diag)]
#[note]
pub struct StringInDiagnostic;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a little disappointed that this doesn't contain a String ☺️


#[derive(LintDiagnostic)]
#[diag(lint_untranslatable_diag)]
pub struct UntranslatableDiag;
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_span/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,7 @@ symbols! {
derive_default_enum,
destruct,
destructuring_assignment,
diag,
diagnostic,
direct,
discriminant_kind,
Expand Down