Skip to content

Commit e56df5e

Browse files
committed
Move some more early buffered lints to dyn lint diagnostics
1 parent 0f48720 commit e56df5e

File tree

10 files changed

+58
-94
lines changed

10 files changed

+58
-94
lines changed

compiler/rustc_builtin_macros/messages.ftl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,11 @@ builtin_macros_naked_functions_testing_attribute =
270270
.label = function marked with testing attribute here
271271
.naked_attribute = `#[unsafe(naked)]` is incompatible with testing attributes
272272
273+
builtin_macros_named_argument_used_positionally = named argument `{$named_arg_name}` is not used by name
274+
.label_named_arg = this named argument is referred to by position in formatting string
275+
.label_position_arg = this formatting argument uses named argument `{$named_arg_name}` by position
276+
.suggestion = use the named argument by name to avoid ambiguity
277+
273278
builtin_macros_no_default_variant = `#[derive(Default)]` on enum with no `#[default]`
274279
.label = this enum needs a unit variant marked with `#[default]`
275280
.suggestion = make this unit variant default by placing `#[default]` on it

compiler/rustc_builtin_macros/src/errors.rs

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
use rustc_errors::codes::*;
22
use rustc_errors::{
3-
Diag, DiagCtxtHandle, Diagnostic, EmissionGuarantee, Level, MultiSpan, SingleLabelManySpans,
4-
Subdiagnostic,
3+
Applicability, Diag, DiagCtxtHandle, Diagnostic, EmissionGuarantee, Level, LintDiagnostic,
4+
MultiSpan, SingleLabelManySpans, Subdiagnostic,
55
};
66
use rustc_macros::{Diagnostic, LintDiagnostic, Subdiagnostic};
77
use rustc_span::{Ident, Span, Symbol};
88

9+
use crate::fluent_generated as fluent;
10+
911
#[derive(LintDiagnostic)]
1012
#[diag(builtin_macros_avoid_intel_syntax)]
1113
pub(crate) struct AvoidIntelSyntax;
@@ -1058,3 +1060,35 @@ pub(crate) struct EiiMacroExpectedMaxOneArgument {
10581060
pub span: Span,
10591061
pub name: String,
10601062
}
1063+
1064+
pub(crate) struct NamedArgumentUsedPositionally {
1065+
pub position_sp_to_replace: Option<Span>,
1066+
pub position_sp_for_msg: Option<Span>,
1067+
pub named_arg_sp: Span,
1068+
pub named_arg_name: Symbol,
1069+
pub is_formatting_arg: bool,
1070+
}
1071+
1072+
impl<'a> LintDiagnostic<'a, ()> for NamedArgumentUsedPositionally {
1073+
fn decorate_lint<'b>(self, diag: &'b mut Diag<'a, ()>) {
1074+
diag.primary_message(fluent::builtin_macros_named_argument_used_positionally);
1075+
diag.span_label(self.named_arg_sp, fluent::builtin_macros_label_named_arg);
1076+
if let Some(span) = self.position_sp_for_msg {
1077+
diag.span_label(span, fluent::builtin_macros_label_position_arg);
1078+
}
1079+
diag.arg("named_arg_name", self.named_arg_name);
1080+
1081+
if let Some(positional_arg_to_replace) = self.position_sp_to_replace {
1082+
let mut name = self.named_arg_name.to_string();
1083+
if self.is_formatting_arg {
1084+
name.push('$')
1085+
};
1086+
diag.span_suggestion_verbose(
1087+
positional_arg_to_replace,
1088+
fluent::builtin_macros_suggestion,
1089+
name,
1090+
Applicability::MaybeIncorrect,
1091+
);
1092+
}
1093+
}
1094+
}

compiler/rustc_builtin_macros/src/format.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ use rustc_errors::{
1414
pluralize,
1515
};
1616
use rustc_expand::base::*;
17+
use rustc_lint_defs::LintId;
1718
use rustc_lint_defs::builtin::NAMED_ARGUMENTS_USED_POSITIONALLY;
18-
use rustc_lint_defs::{BuiltinLintDiag, LintId};
1919
use rustc_parse::exp;
2020
use rustc_parse_format as parse;
2121
use rustc_span::{BytePos, ErrorGuaranteed, Ident, InnerSpan, Span, Symbol};
@@ -589,11 +589,11 @@ fn make_format_args(
589589
span: Some(arg_name.span.into()),
590590
node_id: rustc_ast::CRATE_NODE_ID,
591591
lint_id: LintId::of(NAMED_ARGUMENTS_USED_POSITIONALLY),
592-
diagnostic: BuiltinLintDiag::NamedArgumentUsedPositionally {
592+
diagnostic: errors::NamedArgumentUsedPositionally {
593593
position_sp_to_replace,
594594
position_sp_for_msg,
595595
named_arg_sp: arg_name.span,
596-
named_arg_name: arg_name.name.to_string(),
596+
named_arg_name: arg_name.name,
597597
is_formatting_arg: matches!(used_as, Width | Precision),
598598
}
599599
.into(),

compiler/rustc_lint/messages.ftl

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -596,11 +596,6 @@ lint_mixed_script_confusables =
596596
597597
lint_multiple_supertrait_upcastable = `{$ident}` is dyn-compatible and has multiple supertraits
598598
599-
lint_named_argument_used_positionally = named argument `{$named_arg_name}` is not used by name
600-
.label_named_arg = this named argument is referred to by position in formatting string
601-
.label_position_arg = this formatting argument uses named argument `{$named_arg_name}` by position
602-
.suggestion = use the named argument by name to avoid ambiguity
603-
604599
lint_node_source = `forbid` level set here
605600
.note = {$reason}
606601
@@ -955,9 +950,6 @@ lint_unused_coroutine =
955950
}{$post} that must be used
956951
.note = coroutines are lazy and do nothing unless resumed
957952
958-
lint_unused_crate_dependency = extern crate `{$extern_crate}` is unused in crate `{$local_crate}`
959-
.help = remove the dependency or add `use {$extern_crate} as _;` to the crate root
960-
961953
lint_unused_def = unused {$pre}`{$def}`{$post} that must be used
962954
.suggestion = use `let _ = ...` to ignore the resulting value
963955

compiler/rustc_lint/src/early/diagnostics.rs

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -135,41 +135,6 @@ pub fn decorate_builtin_lint(
135135
BuiltinLintDiag::SingleUseLifetime { use_span: None, deletion_span, ident, .. } => {
136136
lints::UnusedLifetime { deletion_span, ident }.decorate_lint(diag);
137137
}
138-
BuiltinLintDiag::NamedArgumentUsedPositionally {
139-
position_sp_to_replace,
140-
position_sp_for_msg,
141-
named_arg_sp,
142-
named_arg_name,
143-
is_formatting_arg,
144-
} => {
145-
let (suggestion, name) = if let Some(positional_arg_to_replace) = position_sp_to_replace
146-
{
147-
let mut name = named_arg_name.clone();
148-
if is_formatting_arg {
149-
name.push('$')
150-
};
151-
let span_to_replace = if let Ok(positional_arg_content) =
152-
sess.source_map().span_to_snippet(positional_arg_to_replace)
153-
&& positional_arg_content.starts_with(':')
154-
{
155-
positional_arg_to_replace.shrink_to_lo()
156-
} else {
157-
positional_arg_to_replace
158-
};
159-
(Some(span_to_replace), name)
160-
} else {
161-
(None, String::new())
162-
};
163-
164-
lints::NamedArgumentUsedPositionally {
165-
named_arg_sp,
166-
position_label_sp: position_sp_for_msg,
167-
suggestion,
168-
name,
169-
named_arg_name,
170-
}
171-
.decorate_lint(diag);
172-
}
173138
BuiltinLintDiag::AmbiguousGlobReexports {
174139
name,
175140
namespace,
@@ -217,9 +182,6 @@ pub fn decorate_builtin_lint(
217182
}
218183
.decorate_lint(diag);
219184
}
220-
BuiltinLintDiag::UnusedCrateDependency { extern_crate, local_crate } => {
221-
lints::UnusedCrateDependency { extern_crate, local_crate }.decorate_lint(diag)
222-
}
223185
BuiltinLintDiag::AttributeLint(kind) => decorate_attribute_lint(sess, tcx, &kind, diag),
224186
}
225187
}

compiler/rustc_lint/src/lints.rs

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2585,14 +2585,6 @@ pub(crate) mod unexpected_cfg_value {
25852585
}
25862586
}
25872587

2588-
#[derive(LintDiagnostic)]
2589-
#[diag(lint_unused_crate_dependency)]
2590-
#[help]
2591-
pub(crate) struct UnusedCrateDependency {
2592-
pub extern_crate: Symbol,
2593-
pub local_crate: Symbol,
2594-
}
2595-
25962588
// FIXME(jdonszelmann): duplicated in rustc_attr_parsing, should be moved there completely.
25972589
#[derive(LintDiagnostic)]
25982590
#[diag(lint_ill_formed_attribute_input)]
@@ -2718,20 +2710,6 @@ pub(crate) struct UnusedLifetime {
27182710
pub ident: Ident,
27192711
}
27202712

2721-
#[derive(LintDiagnostic)]
2722-
#[diag(lint_named_argument_used_positionally)]
2723-
pub(crate) struct NamedArgumentUsedPositionally {
2724-
#[label(lint_label_named_arg)]
2725-
pub named_arg_sp: Span,
2726-
#[label(lint_label_position_arg)]
2727-
pub position_label_sp: Option<Span>,
2728-
#[suggestion(style = "verbose", code = "{name}", applicability = "maybe-incorrect")]
2729-
pub suggestion: Option<Span>,
2730-
2731-
pub name: String,
2732-
pub named_arg_name: String,
2733-
}
2734-
27352713
#[derive(LintDiagnostic)]
27362714
#[diag(lint_ambiguous_glob_reexport)]
27372715
pub(crate) struct AmbiguousGlobReexports {

compiler/rustc_lint_defs/src/lib.rs

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -684,19 +684,6 @@ pub enum BuiltinLintDiag {
684684
use_span: Option<(Span, bool)>,
685685
ident: Ident,
686686
},
687-
NamedArgumentUsedPositionally {
688-
/// Span where the named argument is used by position and will be replaced with the named
689-
/// argument name
690-
position_sp_to_replace: Option<Span>,
691-
/// Span where the named argument is used by position and is used for lint messages
692-
position_sp_for_msg: Option<Span>,
693-
/// Span where the named argument's name is (so we know where to put the warning message)
694-
named_arg_sp: Span,
695-
/// String containing the named arguments name
696-
named_arg_name: String,
697-
/// Indicates if the named argument is used as a width/precision for formatting
698-
is_formatting_arg: bool,
699-
},
700687
AmbiguousGlobReexports {
701688
/// The name for which collision(s) have occurred.
702689
name: String,
@@ -726,10 +713,6 @@ pub enum BuiltinLintDiag {
726713
span: Span,
727714
lifetimes_in_scope: MultiSpan,
728715
},
729-
UnusedCrateDependency {
730-
extern_crate: Symbol,
731-
local_crate: Symbol,
732-
},
733716
AttributeLint(AttributeLintKind),
734717
}
735718

compiler/rustc_metadata/messages.ftl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,5 +236,8 @@ metadata_two_panic_runtimes =
236236
237237
metadata_unknown_target_modifier_unsafe_allowed = unknown target modifier `{$flag_name}`, requested by `-Cunsafe-allow-abi-mismatch={$flag_name}`
238238
239+
metadata_unused_crate_dependency = extern crate `{$extern_crate}` is unused in crate `{$local_crate}`
240+
.help = remove the dependency or add `use {$extern_crate} as _;` to the crate root
241+
239242
metadata_wasm_c_abi =
240243
older versions of the `wasm-bindgen` crate are incompatible with current versions of Rust; please update to `wasm-bindgen` v0.2.88

compiler/rustc_metadata/src/creader.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,14 @@ use rustc_middle::bug;
2323
use rustc_middle::ty::data_structures::IndexSet;
2424
use rustc_middle::ty::{TyCtxt, TyCtxtFeed};
2525
use rustc_proc_macro::bridge::client::ProcMacro;
26-
use rustc_session::Session;
2726
use rustc_session::config::{
2827
CrateType, ExtendedTargetModifierInfo, ExternLocation, Externs, OptionsTargetModifiers,
2928
TargetModifier,
3029
};
3130
use rustc_session::cstore::{CrateDepKind, CrateSource, ExternCrate, ExternCrateSource};
32-
use rustc_session::lint::{self, BuiltinLintDiag};
3331
use rustc_session::output::validate_crate_name;
3432
use rustc_session::search_paths::PathKind;
33+
use rustc_session::{Session, lint};
3534
use rustc_span::def_id::DefId;
3635
use rustc_span::edition::Edition;
3736
use rustc_span::{DUMMY_SP, Ident, Span, Symbol, sym};
@@ -1205,7 +1204,7 @@ impl CStore {
12051204
lint::builtin::UNUSED_CRATE_DEPENDENCIES,
12061205
span,
12071206
ast::CRATE_NODE_ID,
1208-
BuiltinLintDiag::UnusedCrateDependency {
1207+
errors::UnusedCrateDependency {
12091208
extern_crate: name_interned,
12101209
local_crate: tcx.crate_name(LOCAL_CRATE),
12111210
},

compiler/rustc_metadata/src/errors.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::path::{Path, PathBuf};
33

44
use rustc_errors::codes::*;
55
use rustc_errors::{Diag, DiagCtxtHandle, Diagnostic, EmissionGuarantee, Level};
6-
use rustc_macros::{Diagnostic, Subdiagnostic};
6+
use rustc_macros::{Diagnostic, LintDiagnostic, Subdiagnostic};
77
use rustc_span::{Span, Symbol, sym};
88
use rustc_target::spec::{PanicStrategy, TargetTuple};
99

@@ -615,3 +615,11 @@ pub struct RawDylibMalformed {
615615
#[primary_span]
616616
pub span: Span,
617617
}
618+
619+
#[derive(LintDiagnostic)]
620+
#[diag(metadata_unused_crate_dependency)]
621+
#[help]
622+
pub(crate) struct UnusedCrateDependency {
623+
pub extern_crate: Symbol,
624+
pub local_crate: Symbol,
625+
}

0 commit comments

Comments
 (0)