Skip to content

Commit d2aad59

Browse files
author
Oliver Schneider
committed
lint on statics that could be replaced by constants
1 parent 2c8d75d commit d2aad59

File tree

34 files changed

+108
-76
lines changed

34 files changed

+108
-76
lines changed

src/etc/ziggurat_tables.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ def exp_f_inv(y):
8787

8888
def render_static(name, type, value):
8989
# no space or
90-
return 'pub static %s: %s =%s;\n' % (name, type, value)
90+
return 'pub const %s: %s =%s;\n' % (name, type, value)
9191

9292
# static `name`: [`type`, .. `len(values)`] =
9393
# [values[0], ..., values[3],
@@ -101,7 +101,7 @@ def render_table(name, values):
101101
rows.append(', '.join('%.18f' % f for f in row))
102102

103103
rendered = '\n [%s]' % ',\n '.join(rows)
104-
return render_static(name, '[f64, .. %d]' % len(values), rendered)
104+
return render_static(name, 'ZigTable' % len(values), rendered)
105105

106106

107107
with open('ziggurat_tables.rs', 'w') as f:

src/liballoc/boxed.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ use core::raw::{TraitObject};
8181
#[lang = "exchange_heap"]
8282
#[unstable(feature = "alloc",
8383
reason = "may be renamed; uncertain about custom allocator design")]
84-
pub static HEAP: () = ();
84+
pub const HEAP: () = ();
8585

8686
/// A pointer type for heap allocation.
8787
///

src/libcollections/bit.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,8 @@ fn match_words <'a,'b>(a: &'a BitVec, b: &'b BitVec) -> (MatchWords<'a>, MatchWo
125125
}
126126
}
127127

128-
static TRUE: bool = true;
129-
static FALSE: bool = false;
128+
const TRUE: &'static bool = &true;
129+
const FALSE: &'static bool = &false;
130130

131131
/// The bitvector type.
132132
///
@@ -172,9 +172,9 @@ impl Index<usize> for BitVec {
172172
#[inline]
173173
fn index(&self, i: usize) -> &bool {
174174
if self.get(i).expect("index out of bounds") {
175-
&TRUE
175+
TRUE
176176
} else {
177-
&FALSE
177+
FALSE
178178
}
179179
}
180180
}

src/libcollections/vec.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ use borrow::{Cow, IntoCow};
8383
use super::range::RangeArgument;
8484

8585
// FIXME- fix places which assume the max vector allowed has memory usize::MAX.
86-
static MAX_MEMORY_SIZE: usize = isize::MAX as usize;
86+
const MAX_MEMORY_SIZE: usize = isize::MAX as usize;
8787

8888
/// A growable list type, written `Vec<T>` but pronounced 'vector.'
8989
///

src/libcore/macros.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@ macro_rules! panic {
1515
panic!("explicit panic")
1616
);
1717
($msg:expr) => ({
18-
static _MSG_FILE_LINE: (&'static str, &'static str, u32) = ($msg, file!(), line!());
18+
const _MSG_FILE_LINE: (&'static str, &'static str, u32) = ($msg, file!(), line!());
1919
::core::panicking::panic(&_MSG_FILE_LINE)
2020
});
2121
($fmt:expr, $($arg:tt)*) => ({
2222
// The leading _'s are to avoid dead code warnings if this is
2323
// used inside a dead function. Just `#[allow(dead_code)]` is
2424
// insufficient, since the user may have
2525
// `#[forbid(dead_code)]` and which cannot be overridden.
26-
static _FILE_LINE: (&'static str, u32) = (file!(), line!());
26+
const _FILE_LINE: (&'static str, u32) = (file!(), line!());
2727
::core::panicking::panic_fmt(format_args!($fmt, $($arg)*), &_FILE_LINE)
2828
});
2929
}

src/libcore/num/flt2dec/strategy/dragon.rs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,19 @@ use num::flt2dec::estimator::estimate_scaling_factor;
2424
use num::flt2dec::bignum::Digit32 as Digit;
2525
use num::flt2dec::bignum::Big32x36 as Big;
2626

27-
// FIXME(#22540) const ref to static array seems to ICE
28-
static POW10: [Digit; 10] = [1, 10, 100, 1000, 10000, 100000,
29-
1000000, 10000000, 100000000, 1000000000];
30-
static TWOPOW10: [Digit; 10] = [2, 20, 200, 2000, 20000, 200000,
31-
2000000, 20000000, 200000000, 2000000000];
27+
const POW10: [Digit; 10] = [1, 10, 100, 1000, 10000, 100000,
28+
1000000, 10000000, 100000000, 1000000000];
29+
const TWOPOW10: [Digit; 10] = [2, 20, 200, 2000, 20000, 200000,
30+
2000000, 20000000, 200000000, 2000000000];
3231

3332
// precalculated arrays of `Digit`s for 10^(2^n)
34-
static POW10TO16: [Digit; 2] = [0x6fc10000, 0x2386f2];
35-
static POW10TO32: [Digit; 4] = [0, 0x85acef81, 0x2d6d415b, 0x4ee];
36-
static POW10TO64: [Digit; 7] = [0, 0, 0xbf6a1f01, 0x6e38ed64, 0xdaa797ed, 0xe93ff9f4, 0x184f03];
37-
static POW10TO128: [Digit; 14] =
33+
const POW10TO16: [Digit; 2] = [0x6fc10000, 0x2386f2];
34+
const POW10TO32: [Digit; 4] = [0, 0x85acef81, 0x2d6d415b, 0x4ee];
35+
const POW10TO64: [Digit; 7] = [0, 0, 0xbf6a1f01, 0x6e38ed64, 0xdaa797ed, 0xe93ff9f4, 0x184f03];
36+
const POW10TO128: [Digit; 14] =
3837
[0, 0, 0, 0, 0x2e953e01, 0x3df9909, 0xf1538fd, 0x2374e42f, 0xd3cff5ec, 0xc404dc08,
3938
0xbccdb0da, 0xa6337f19, 0xe91f2603, 0x24e];
40-
static POW10TO256: [Digit; 27] =
39+
const POW10TO256: [Digit; 27] =
4140
[0, 0, 0, 0, 0, 0, 0, 0, 0x982e7c01, 0xbed3875b, 0xd8d99f72, 0x12152f87, 0x6bde50c6,
4241
0xcf4a6e70, 0xd595d80f, 0x26b2716e, 0xadc666b0, 0x1d153624, 0x3c42d35a, 0x63ff540e,
4342
0xcc5573c0, 0x65f9ef17, 0x55bc28f2, 0x80dcc7f7, 0xf46eeddc, 0x5fdcefce, 0x553f7];
@@ -328,4 +327,3 @@ pub fn format_exact(d: &Decoded, buf: &mut [u8], limit: i16) -> (/*#digits*/ usi
328327

329328
(len, k)
330329
}
331-

src/libcore/num/flt2dec/strategy/grisu.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,9 @@ for i in xrange(-308, 333, 8):
8787
f = ((f << 64 >> (l-1)) + 1) >> 1; e += l - 64
8888
print ' (%#018x, %5d, %4d),' % (f, e, i)
8989
*/
90-
// FIXME(#22540) const ref to static array seems to ICE
90+
9191
#[doc(hidden)]
92-
pub static CACHED_POW10: [(u64, i16, i16); 81] = [ // (f, e, k)
92+
pub const CACHED_POW10: [(u64, i16, i16); 81] = [ // (f, e, k)
9393
(0xe61acf033d1a45df, -1087, -308),
9494
(0xab70fe17c79ac6ca, -1060, -300),
9595
(0xff77b1fcbebcdc4f, -1034, -292),
@@ -746,4 +746,3 @@ pub fn format_exact(d: &Decoded, buf: &mut [u8], limit: i16) -> (/*#digits*/ usi
746746
None => fallback(d, buf, limit),
747747
}
748748
}
749-

src/libcore/str/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1282,7 +1282,7 @@ fn run_utf8_validation_iterator(iter: &mut slice::Iter<u8>)
12821282
}
12831283

12841284
// https://tools.ietf.org/html/rfc3629
1285-
static UTF8_CHAR_WIDTH: [u8; 256] = [
1285+
const UTF8_CHAR_WIDTH: [u8; 256] = [
12861286
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
12871287
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, // 0x1F
12881288
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,

src/liblog/directive.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub struct LogDirective {
1717
pub level: u32,
1818
}
1919

20-
pub static LOG_LEVEL_NAMES: [&'static str; 4] = ["ERROR", "WARN", "INFO",
20+
pub const LOG_LEVEL_NAMES: [&'static str; 4] = ["ERROR", "WARN", "INFO",
2121
"DEBUG"];
2222

2323
/// Parse an individual log level that is either a number or a symbolic log level

src/liblog/macros.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,14 @@
5050
#[macro_export]
5151
macro_rules! log {
5252
($lvl:expr, $($arg:tt)+) => ({
53-
static LOC: ::log::LogLocation = ::log::LogLocation {
53+
const LOC: &'static ::log::LogLocation = &::log::LogLocation {
5454
line: line!(),
5555
file: file!(),
5656
module_path: module_path!(),
5757
};
5858
let lvl = $lvl;
5959
if log_enabled!(lvl) {
60-
::log::log(lvl, &LOC, format_args!($($arg)+))
60+
::log::log(lvl, LOC, format_args!($($arg)+))
6161
}
6262
})
6363
}

src/librand/chacha.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ pub struct ChaChaRng {
3333
index: usize, // Index into state
3434
}
3535

36-
static EMPTY: ChaChaRng = ChaChaRng {
36+
const EMPTY: ChaChaRng = ChaChaRng {
3737
buffer: [0; STATE_WORDS],
3838
state: [0; STATE_WORDS],
3939
index: STATE_WORDS

src/librand/distributions/exponential.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ impl Rand for Exp1 {
4646
}
4747

4848
Exp1(ziggurat(rng, false,
49-
&ziggurat_tables::ZIG_EXP_X,
50-
&ziggurat_tables::ZIG_EXP_F,
49+
ziggurat_tables::ZIG_EXP_X,
50+
ziggurat_tables::ZIG_EXP_F,
5151
pdf, zero_case))
5252
}
5353
}

src/librand/distributions/normal.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ impl Rand for StandardNormal {
6262
StandardNormal(ziggurat(
6363
rng,
6464
true, // this is symmetric
65-
&ziggurat_tables::ZIG_NORM_X,
66-
&ziggurat_tables::ZIG_NORM_F,
65+
ziggurat_tables::ZIG_NORM_X,
66+
ziggurat_tables::ZIG_NORM_F,
6767
pdf, zero_case))
6868
}
6969
}

src/librand/distributions/ziggurat_tables.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
// algorithm. Autogenerated by `ziggurat_tables.py`.
1313

1414
pub type ZigTable = &'static [f64; 257];
15-
pub static ZIG_NORM_R: f64 = 3.654152885361008796;
16-
pub static ZIG_NORM_X: [f64; 257] =
15+
pub const ZIG_NORM_R: f64 = 3.654152885361008796;
16+
pub const ZIG_NORM_X: ZigTable = &
1717
[3.910757959537090045, 3.654152885361008796, 3.449278298560964462, 3.320244733839166074,
1818
3.224575052047029100, 3.147889289517149969, 3.083526132001233044, 3.027837791768635434,
1919
2.978603279880844834, 2.934366867207854224, 2.894121053612348060, 2.857138730872132548,
@@ -79,7 +79,7 @@ pub static ZIG_NORM_X: [f64; 257] =
7979
0.487443966121754335, 0.463634336771763245, 0.437518402186662658, 0.408389134588000746,
8080
0.375121332850465727, 0.335737519180459465, 0.286174591747260509, 0.215241895913273806,
8181
0.000000000000000000];
82-
pub static ZIG_NORM_F: [f64; 257] =
82+
pub const ZIG_NORM_F: ZigTable = &
8383
[0.000477467764586655, 0.001260285930498598, 0.002609072746106363, 0.004037972593371872,
8484
0.005522403299264754, 0.007050875471392110, 0.008616582769422917, 0.010214971439731100,
8585
0.011842757857943104, 0.013497450601780807, 0.015177088307982072, 0.016880083152595839,
@@ -145,8 +145,8 @@ pub static ZIG_NORM_F: [f64; 257] =
145145
0.887984660763399880, 0.898095921906304051, 0.908726440060562912, 0.919991505048360247,
146146
0.932060075968990209, 0.945198953453078028, 0.959879091812415930, 0.977101701282731328,
147147
1.000000000000000000];
148-
pub static ZIG_EXP_R: f64 = 7.697117470131050077;
149-
pub static ZIG_EXP_X: [f64; 257] =
148+
pub const ZIG_EXP_R: f64 = 7.697117470131050077;
149+
pub const ZIG_EXP_X: ZigTable = &
150150
[8.697117470131052741, 7.697117470131050077, 6.941033629377212577, 6.478378493832569696,
151151
6.144164665772472667, 5.882144315795399869, 5.666410167454033697, 5.482890627526062488,
152152
5.323090505754398016, 5.181487281301500047, 5.054288489981304089, 4.938777085901250530,
@@ -212,7 +212,7 @@ pub static ZIG_EXP_X: [f64; 257] =
212212
0.253658363385912022, 0.233790483059674731, 0.212671510630966620, 0.189958689622431842,
213213
0.165127622564187282, 0.137304980940012589, 0.104838507565818778, 0.063852163815001570,
214214
0.000000000000000000];
215-
pub static ZIG_EXP_F: [f64; 257] =
215+
pub const ZIG_EXP_F: ZigTable = &
216216
[0.000167066692307963, 0.000454134353841497, 0.000967269282327174, 0.001536299780301573,
217217
0.002145967743718907, 0.002788798793574076, 0.003460264777836904, 0.004157295120833797,
218218
0.004877655983542396, 0.005619642207205489, 0.006381905937319183, 0.007163353183634991,

src/librand/isaac.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ pub struct IsaacRng {
4545
c: w32,
4646
}
4747

48-
static EMPTY: IsaacRng = IsaacRng {
48+
const EMPTY: IsaacRng = IsaacRng {
4949
cnt: 0,
5050
rsl: [w(0); RAND_SIZE_USIZE],
5151
mem: [w(0); RAND_SIZE_USIZE],
@@ -288,7 +288,7 @@ pub struct Isaac64Rng {
288288
c: w64,
289289
}
290290

291-
static EMPTY_64: Isaac64Rng = Isaac64Rng {
291+
const EMPTY_64: Isaac64Rng = Isaac64Rng {
292292
cnt: 0,
293293
rsl: [w(0); RAND_SIZE_64],
294294
mem: [w(0); RAND_SIZE_64],

src/librbml/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ pub enum EbmlEncoderTag {
209209
const NUM_TAGS: usize = 0x1000;
210210
const NUM_IMPLICIT_TAGS: usize = 0x0e;
211211

212-
static TAG_IMPLICIT_LEN: [i8; NUM_IMPLICIT_TAGS] = [
212+
const TAG_IMPLICIT_LEN: [i8; NUM_IMPLICIT_TAGS] = [
213213
1, 2, 4, 8, // EsU*
214214
1, 2, 4, 8, // ESI*
215215
1, // EsBool
@@ -334,7 +334,7 @@ pub mod reader {
334334
// most significant bit is set etc. we can replace up to three
335335
// "and+branch" with a single table lookup which gives us a measured
336336
// speedup of around 2x on x86_64.
337-
static SHIFT_MASK_TABLE: [(usize, u32); 16] = [
337+
const SHIFT_MASK_TABLE: [(usize, u32); 16] = [
338338
(0, 0x0), (0, 0x0fffffff),
339339
(8, 0x1fffff), (8, 0x1fffff),
340340
(16, 0x3fff), (16, 0x3fff), (16, 0x3fff), (16, 0x3fff),

src/librustc/lint/mod.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,14 @@ macro_rules! lint_initializer {
8888
macro_rules! declare_lint {
8989
// FIXME(#14660): deduplicate
9090
(pub $name:ident, $level:ident, $desc:expr) => (
91+
// cannot be const, otherwise random lints fail to lint
92+
#[cfg_attr(not(stage0), allow(static_could_be_const))]
9193
pub static $name: &'static ::rustc::lint::Lint
9294
= &lint_initializer!($name, $level, $desc);
9395
);
9496
($name:ident, $level:ident, $desc:expr) => (
97+
// cannot be const, otherwise random lints fail to lint
98+
#[cfg_attr(not(stage0), allow(static_could_be_const))]
9599
static $name: &'static ::rustc::lint::Lint
96100
= &lint_initializer!($name, $level, $desc);
97101
);
@@ -101,9 +105,9 @@ macro_rules! declare_lint {
101105
#[macro_export]
102106
macro_rules! lint_array { ($( $lint:expr ),*) => (
103107
{
104-
#[allow(non_upper_case_globals)]
105-
static array: LintArray = &[ $( &$lint ),* ];
106-
array
108+
#[cfg_attr(not(stage0), allow(static_could_be_const))]
109+
static ARRAY: LintArray = &[ $( &$lint ),* ];
110+
ARRAY
107111
}
108112
) }
109113

src/librustc/middle/resolve_lifetime.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,14 +91,14 @@ enum ScopeChain<'a> {
9191

9292
type Scope<'a> = &'a ScopeChain<'a>;
9393

94-
static ROOT_SCOPE: ScopeChain<'static> = RootScope;
94+
const ROOT_SCOPE: &'static ScopeChain<'static> = &RootScope;
9595

9696
pub fn krate(sess: &Session, krate: &ast::Crate, def_map: &DefMap) -> NamedRegionMap {
9797
let mut named_region_map = NodeMap();
9898
visit::walk_crate(&mut LifetimeContext {
9999
sess: sess,
100100
named_region_map: &mut named_region_map,
101-
scope: &ROOT_SCOPE,
101+
scope: ROOT_SCOPE,
102102
def_map: def_map,
103103
trait_ref_hack: false,
104104
labels_in_fn: vec![],

src/librustc_back/sha2.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ impl Engine256State {
407407
}
408408
}
409409

410-
static K32: [u32; 64] = [
410+
const K32: [u32; 64] = [
411411
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
412412
0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
413413
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
@@ -514,7 +514,7 @@ impl Digest for Sha256 {
514514
fn output_bits(&self) -> usize { 256 }
515515
}
516516

517-
static H256: [u32; 8] = [
517+
const H256: [u32; 8] = [
518518
0x6a09e667,
519519
0xbb67ae85,
520520
0x3c6ef372,

src/librustc_lint/builtin.rs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ use middle::subst::Substs;
3434
use middle::ty::{self, Ty};
3535
use middle::{def, pat_util, stability};
3636
use middle::const_eval::{eval_const_expr_partial, const_int, const_uint};
37-
use middle::cfg;
37+
use middle::{cfg, check_const};
3838
use util::ppaux::ty_to_string;
3939
use util::nodemap::{FnvHashMap, NodeSet};
4040
use lint::{Level, Context, LintPass, LintArray, Lint};
@@ -107,6 +107,33 @@ declare_lint! {
107107
"shift exceeds the type's number of bits"
108108
}
109109

110+
declare_lint! {
111+
STATIC_COULD_BE_CONST,
112+
Warn,
113+
"static declaration could be replaced by a const declaration"
114+
}
115+
116+
#[derive(Copy, Clone)]
117+
pub struct StaticCouldBeConst;
118+
119+
impl LintPass for StaticCouldBeConst {
120+
fn get_lints(&self) -> LintArray {
121+
lint_array!(STATIC_COULD_BE_CONST)
122+
}
123+
124+
fn check_item(&mut self, cx: &Context, it: &ast::Item) {
125+
let expr = match it.node {
126+
ast::ItemStatic(_, ast::MutImmutable, ref expr) => expr,
127+
_ => return,
128+
};
129+
let const_qualif = cx.tcx.const_qualif_map.borrow()[&expr.id];
130+
if const_qualif.contains(check_const::ConstQualif::MUTABLE_MEM) {
131+
return;
132+
}
133+
cx.span_lint(STATIC_COULD_BE_CONST, it.span, "static could be replaced by a const");
134+
}
135+
}
136+
110137
#[derive(Copy, Clone)]
111138
pub struct TypeLimits {
112139
/// Id of the last visited negated expression

src/librustc_lint/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ pub fn register_builtins(store: &mut lint::LintStore, sess: Option<&Session>) {
110110
PluginAsLibrary,
111111
DropWithReprExtern,
112112
MutableTransmutes,
113+
StaticCouldBeConst,
113114
);
114115

115116
add_builtin_with_new!(sess,

src/librustc_trans/trans/builder.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ pub struct Builder<'a, 'tcx: 'a> {
3333
// This is a really awful way to get a zero-length c-string, but better (and a
3434
// lot more efficient) than doing str::as_c_str("", ...) every time.
3535
pub fn noname() -> *const c_char {
36-
static CNULL: c_char = 0;
37-
&CNULL
36+
const CNULL: &'static c_char = &0;
37+
CNULL
3838
}
3939

4040
impl<'a, 'tcx> Builder<'a, 'tcx> {

0 commit comments

Comments
 (0)