diff --git a/src/librustc_lint/builtin.rs b/src/librustc_lint/builtin.rs index a2a6a0da68ff055034ff393c6b5c211cb706db2f..ffb844632e46fe54e982b7add61c2c401b61fccf 100644 --- a/src/librustc_lint/builtin.rs +++ b/src/librustc_lint/builtin.rs @@ -1214,8 +1214,13 @@ fn check_item(&mut self, cx: &LateContext, it: &hir::Item) { // don't have anything to attach a symbol to let msg = "const items should never be #[no_mangle]"; let mut err = cx.struct_span_lint(NO_MANGLE_CONST_ITEMS, it.span, msg); + + // account for "pub const" (#45562) + let start = cx.tcx.sess.codemap().span_to_snippet(it.span) + .map(|snippet| snippet.find("const").unwrap_or(0)) + .unwrap_or(0) as u32; // `const` is 5 chars - let const_span = it.span.with_hi(BytePos(it.span.lo().0 + 5)); + let const_span = it.span.with_hi(BytePos(it.span.lo().0 + start + 5)); err.span_suggestion(const_span, "try a static value", "pub static".to_owned()); diff --git a/src/test/ui/suggestions/issue-45562.rs b/src/test/ui/suggestions/issue-45562.rs new file mode 100644 index 0000000000000000000000000000000000000000..f493df56f949d9e06d3fa37f0e4105ded9d1d714 --- /dev/null +++ b/src/test/ui/suggestions/issue-45562.rs @@ -0,0 +1,14 @@ +// Copyright 2018 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#[no_mangle] pub const RAH: usize = 5; +//~^ ERROR const items should never be #[no_mangle] + +fn main() {} diff --git a/src/test/ui/suggestions/issue-45562.stderr b/src/test/ui/suggestions/issue-45562.stderr new file mode 100644 index 0000000000000000000000000000000000000000..2f8c4cd3f2e72fd61a2fb6292a0acdf7ec50ffe4 --- /dev/null +++ b/src/test/ui/suggestions/issue-45562.stderr @@ -0,0 +1,12 @@ +error: const items should never be #[no_mangle] + --> $DIR/issue-45562.rs:11:14 + | +11 | #[no_mangle] pub const RAH: usize = 5; + | ---------^^^^^^^^^^^^^^^^ + | | + | help: try a static value: `pub static` + | + = note: #[deny(no_mangle_const_items)] on by default + +error: aborting due to previous error +