Simplify and robustly compute suggestion span using

`vis_span.to(ident.span.shrink_to_lo())`
This commit is contained in:
reddevilmidzy 2025-12-03 10:06:16 +09:00
parent 6ce0f0ff91
commit 2951d72219
5 changed files with 10 additions and 18 deletions

View file

@ -41,7 +41,7 @@ pub use rustc_session::lint::builtin::*;
use rustc_session::{declare_lint, declare_lint_pass, impl_lint_pass};
use rustc_span::edition::Edition;
use rustc_span::source_map::Spanned;
use rustc_span::{BytePos, DUMMY_SP, Ident, InnerSpan, Span, Symbol, kw, sym};
use rustc_span::{DUMMY_SP, Ident, InnerSpan, Span, Symbol, kw, sym};
use rustc_target::asm::InlineAsmArch;
use rustc_trait_selection::infer::{InferCtxtExt, TyCtxtInferExt};
use rustc_trait_selection::traits::misc::type_allowed_to_implement_copy;
@ -997,20 +997,12 @@ impl<'tcx> LateLintPass<'tcx> for InvalidNoMangleItems {
self.check_no_mangle_on_generic_fn(cx, attr_span, it.owner_id.def_id);
}
}
hir::ItemKind::Const(_, generics, ..) => {
hir::ItemKind::Const(ident, generics, ..) => {
if find_attr!(attrs, AttributeKind::NoMangle(..)) {
let suggestion =
if generics.params.is_empty() && generics.where_clause_span.is_empty() {
// account for "pub const" (#45562)
let start = cx
.tcx
.sess
.source_map()
.span_to_snippet(it.span)
.map(|snippet| snippet.find("const").unwrap_or(0))
.unwrap_or(0) as u32;
// `const` is 5 chars
Some(it.span.with_hi(BytePos(it.span.lo().0 + start + 5)))
Some(it.span.until(ident.span))
} else {
None
};

View file

@ -229,7 +229,7 @@ pub(crate) struct BuiltinNoMangleGeneric {
#[derive(LintDiagnostic)]
#[diag(lint_builtin_const_no_mangle)]
pub(crate) struct BuiltinConstNoMangle {
#[suggestion(code = "pub static", applicability = "machine-applicable")]
#[suggestion(code = "pub static ", applicability = "machine-applicable")]
pub suggestion: Option<Span>,
}

View file

@ -2,7 +2,7 @@ error: const items should never be `#[no_mangle]`
--> $DIR/issue-45562.rs:5:14
|
LL | #[no_mangle] pub const RAH: usize = 5;
| ---------^^^^^^^^^^^^^^^^
| ----------^^^^^^^^^^^^^^^
| |
| help: try a static value: `pub static`
|

View file

@ -31,7 +31,7 @@ error: const items should never be `#[no_mangle]`
--> $DIR/lint-unexported-no-mangle.rs:9:1
|
LL | const FOO: u64 = 1;
| -----^^^^^^^^^^^^^^
| ------^^^^^^^^^^^^^
| |
| help: try a static value: `pub static`
|
@ -41,7 +41,7 @@ error: const items should never be `#[no_mangle]`
--> $DIR/lint-unexported-no-mangle.rs:12:1
|
LL | pub const PUB_FOO: u64 = 1;
| ---------^^^^^^^^^^^^^^^^^^
| ----------^^^^^^^^^^^^^^^^^
| |
| help: try a static value: `pub static`

View file

@ -52,7 +52,7 @@ error: const items should never be `#[no_mangle]`
--> $DIR/suggestions.rs:6:14
|
LL | #[no_mangle] const DISCOVERY: usize = 1;
| -----^^^^^^^^^^^^^^^^^^^^^^
| ------^^^^^^^^^^^^^^^^^^^^^
| |
| help: try a static value: `pub static`
|
@ -81,7 +81,7 @@ error: const items should never be `#[no_mangle]`
--> $DIR/suggestions.rs:22:18
|
LL | #[no_mangle] pub const DAUNTLESS: bool = true;
| ---------^^^^^^^^^^^^^^^^^^^^^^^^
| ----------^^^^^^^^^^^^^^^^^^^^^^^
| |
| help: try a static value: `pub static`
@ -97,7 +97,7 @@ error: const items should never be `#[no_mangle]`
--> $DIR/suggestions.rs:31:18
|
LL | #[no_mangle] pub(crate) const VETAR: bool = true;
| ----------------^^^^^^^^^^^^^^^^^^^^
| -----------------^^^^^^^^^^^^^^^^^^^
| |
| help: try a static value: `pub static`