Skip to content

Commit 0528693

Browse files
committed
Account for pub in const -> static suggestion
1 parent ee220da commit 0528693

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

src/librustc_lint/builtin.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -1214,8 +1214,13 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for InvalidNoMangleItems {
12141214
// don't have anything to attach a symbol to
12151215
let msg = "const items should never be #[no_mangle]";
12161216
let mut err = cx.struct_span_lint(NO_MANGLE_CONST_ITEMS, it.span, msg);
1217+
1218+
// account for "pub const" (#45562)
1219+
let start = cx.tcx.sess.codemap().span_to_snippet(it.span)
1220+
.map(|snippet| snippet.find("const").unwrap_or(0))
1221+
.unwrap_or(0) as u32;
12171222
// `const` is 5 chars
1218-
let const_span = it.span.with_hi(BytePos(it.span.lo().0 + 5));
1223+
let const_span = it.span.with_hi(BytePos(it.span.lo().0 + start + 5));
12191224
err.span_suggestion(const_span,
12201225
"try a static value",
12211226
"pub static".to_owned());
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#[no_mangle] pub const RAH: usize = 5;
12+
//~^ ERROR const items should never be #[no_mangle]
13+
14+
fn main() {}
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error: const items should never be #[no_mangle]
2+
--> $DIR/issue-45562.rs:11:14
3+
|
4+
11 | #[no_mangle] pub const RAH: usize = 5;
5+
| ---------^^^^^^^^^^^^^^^^
6+
| |
7+
| help: try a static value: `pub static`
8+
|
9+
= note: #[deny(no_mangle_const_items)] on by default
10+
11+
error: aborting due to previous error
12+

0 commit comments

Comments
 (0)