Skip to content

Commit 33daff4

Browse files
committed
Move some more early buffered lints to dyn lint diagnostics
1 parent 55a800d commit 33daff4

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
@@ -259,6 +259,11 @@ builtin_macros_naked_functions_testing_attribute =
259259
.label = function marked with testing attribute here
260260
.naked_attribute = `#[unsafe(naked)]` is incompatible with testing attributes
261261
262+
builtin_macros_named_argument_used_positionally = named argument `{$named_arg_name}` is not used by name
263+
.label_named_arg = this named argument is referred to by position in formatting string
264+
.label_position_arg = this formatting argument uses named argument `{$named_arg_name}` by position
265+
.suggestion = use the named argument by name to avoid ambiguity
266+
262267
builtin_macros_no_default_variant = `#[derive(Default)]` on enum with no `#[default]`
263268
.label = this enum needs a unit variant marked with `#[default]`
264269
.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;
@@ -1010,3 +1012,35 @@ pub(crate) struct CfgSelectUnreachable {
10101012
#[label]
10111013
pub wildcard_span: Span,
10121014
}
1015+
1016+
pub(crate) struct NamedArgumentUsedPositionally {
1017+
pub position_sp_to_replace: Option<Span>,
1018+
pub position_sp_for_msg: Option<Span>,
1019+
pub named_arg_sp: Span,
1020+
pub named_arg_name: Symbol,
1021+
pub is_formatting_arg: bool,
1022+
}
1023+
1024+
impl<'a> LintDiagnostic<'a, ()> for NamedArgumentUsedPositionally {
1025+
fn decorate_lint<'b>(self, diag: &'b mut Diag<'a, ()>) {
1026+
diag.primary_message(fluent::builtin_macros_named_argument_used_positionally);
1027+
diag.span_label(self.named_arg_sp, fluent::builtin_macros_label_named_arg);
1028+
if let Some(span) = self.position_sp_for_msg {
1029+
diag.span_label(span, fluent::builtin_macros_label_position_arg);
1030+
}
1031+
diag.arg("named_arg_name", self.named_arg_name);
1032+
1033+
if let Some(positional_arg_to_replace) = self.position_sp_to_replace {
1034+
let mut name = self.named_arg_name.to_string();
1035+
if self.is_formatting_arg {
1036+
name.push('$')
1037+
};
1038+
diag.span_suggestion_verbose(
1039+
positional_arg_to_replace,
1040+
fluent::builtin_macros_suggestion,
1041+
name,
1042+
Applicability::MaybeIncorrect,
1043+
);
1044+
}
1045+
}
1046+
}

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
@@ -954,9 +949,6 @@ lint_unused_coroutine =
954949
}{$post} that must be used
955950
.note = coroutines are lazy and do nothing unless resumed
956951
957-
lint_unused_crate_dependency = extern crate `{$extern_crate}` is unused in crate `{$local_crate}`
958-
.help = remove the dependency or add `use {$extern_crate} as _;` to the crate root
959-
960952
lint_unused_def = unused {$pre}`{$def}`{$post} that must be used
961953
.suggestion = use `let _ = ...` to ignore the resulting value
962954

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
@@ -2574,14 +2574,6 @@ pub(crate) mod unexpected_cfg_value {
25742574
}
25752575
}
25762576

2577-
#[derive(LintDiagnostic)]
2578-
#[diag(lint_unused_crate_dependency)]
2579-
#[help]
2580-
pub(crate) struct UnusedCrateDependency {
2581-
pub extern_crate: Symbol,
2582-
pub local_crate: Symbol,
2583-
}
2584-
25852577
// FIXME(jdonszelmann): duplicated in rustc_attr_parsing, should be moved there completely.
25862578
#[derive(LintDiagnostic)]
25872579
#[diag(lint_ill_formed_attribute_input)]
@@ -2707,20 +2699,6 @@ pub(crate) struct UnusedLifetime {
27072699
pub ident: Ident,
27082700
}
27092701

2710-
#[derive(LintDiagnostic)]
2711-
#[diag(lint_named_argument_used_positionally)]
2712-
pub(crate) struct NamedArgumentUsedPositionally {
2713-
#[label(lint_label_named_arg)]
2714-
pub named_arg_sp: Span,
2715-
#[label(lint_label_position_arg)]
2716-
pub position_label_sp: Option<Span>,
2717-
#[suggestion(style = "verbose", code = "{name}", applicability = "maybe-incorrect")]
2718-
pub suggestion: Option<Span>,
2719-
2720-
pub name: String,
2721-
pub named_arg_name: String,
2722-
}
2723-
27242702
#[derive(LintDiagnostic)]
27252703
#[diag(lint_ambiguous_glob_reexport)]
27262704
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};
@@ -1261,7 +1260,7 @@ impl CStore {
12611260
lint::builtin::UNUSED_CRATE_DEPENDENCIES,
12621261
span,
12631262
ast::CRATE_NODE_ID,
1264-
BuiltinLintDiag::UnusedCrateDependency {
1263+
errors::UnusedCrateDependency {
12651264
extern_crate: name_interned,
12661265
local_crate: tcx.crate_name(LOCAL_CRATE),
12671266
},

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)