Skip to content

Commit 21e7b93

Browse files
author
Keegan McAllister
committed
Use names in Lint structs in an ASCII-case-insensitive way
In preparation for the next commit.
1 parent 609552e commit 21e7b93

File tree

3 files changed

+27
-13
lines changed

3 files changed

+27
-13
lines changed

src/librustc/driver/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ Available lint options:
146146
let mut lints: Vec<_> = lints.move_iter().map(|(x, _)| x).collect();
147147
lints.sort_by(|x: &&Lint, y: &&Lint| {
148148
match x.default_level.cmp(&y.default_level) {
149+
// The sort doesn't case-fold but it's doubtful we care.
149150
Equal => x.name.cmp(&y.name),
150151
r => r,
151152
}
@@ -172,7 +173,7 @@ Available lint options:
172173

173174
let print_lints = |lints: Vec<&Lint>| {
174175
for lint in lints.move_iter() {
175-
let name = lint.name.replace("_", "-");
176+
let name = lint.name_lower().replace("_", "-");
176177
println!(" {} {:7.7s} {}",
177178
padded(name.as_slice()), lint.default_level.as_str(), lint.desc);
178179
}

src/librustc/lint/context.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ pub struct LintStore {
6161
passes: Option<Vec<LintPassObject>>,
6262

6363
/// Lints indexed by name.
64-
by_name: HashMap<&'static str, LintId>,
64+
by_name: HashMap<String, LintId>,
6565

6666
/// Current levels of each lint, and where they were set.
6767
levels: HashMap<LintId, LevelSource>,
@@ -102,8 +102,8 @@ impl LintStore {
102102
self.lints.push((lint, from_plugin));
103103

104104
let id = LintId::of(lint);
105-
if !self.by_name.insert(lint.name, id) {
106-
let msg = format!("duplicate specification of lint {}", lint.name);
105+
if !self.by_name.insert(lint.name_lower(), id) {
106+
let msg = format!("duplicate specification of lint {}", lint.name_lower());
107107
match (sess, from_plugin) {
108108
// We load builtin lints first, so a duplicate is a compiler bug.
109109
// Use early_error when handling -W help with no crate.
@@ -205,18 +205,19 @@ pub fn raw_emit_lint(sess: &Session, lint: &'static Lint,
205205
let (mut level, source) = lvlsrc;
206206
if level == Allow { return }
207207

208+
let name = lint.name_lower();
208209
let mut note = None;
209210
let msg = match source {
210211
Default => {
211212
format!("{}, #[{}({})] on by default", msg,
212-
level.as_str(), lint.name)
213+
level.as_str(), name)
213214
},
214215
CommandLine => {
215216
format!("{} [-{} {}]", msg,
216217
match level {
217218
Warn => 'W', Deny => 'D', Forbid => 'F',
218219
Allow => fail!()
219-
}, lint.name.replace("_", "-"))
220+
}, name.replace("_", "-"))
220221
},
221222
Node(src) => {
222223
note = Some(src);
@@ -355,7 +356,7 @@ impl<'a> Context<'a> {
355356
for meta in metas.iter() {
356357
match meta.node {
357358
ast::MetaWord(ref lint_name) => {
358-
match self.lints.by_name.find_equiv(lint_name) {
359+
match self.lints.by_name.find_equiv(&lint_name.get()) {
359360
Some(lint_id) => out.push((*lint_id, level, meta.span)),
360361

361362
None => self.span_lint(builtin::unrecognized_lint,

src/librustc/lint/mod.rs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
use middle::privacy::ExportedItems;
3333
use std::hash;
34+
use std::ascii::StrAsciiExt;
3435
use syntax::codemap::Span;
3536
use syntax::visit::FnKind;
3637
use syntax::ast;
@@ -41,10 +42,14 @@ pub use lint::context::{Context, LintStore, raw_emit_lint, check_crate};
4142
pub struct Lint {
4243
/// A string identifier for the lint.
4344
///
44-
/// Written with underscores, e.g. "unused_imports".
45-
/// This identifies the lint in attributes and in
46-
/// command-line arguments. On the command line,
47-
/// underscores become dashes.
45+
/// This identifies the lint in attributes and in command-line arguments.
46+
/// In those contexts it is always lowercase, but this field is compared
47+
/// in a way which is case-insensitive for ASCII characters. This allows
48+
/// `declare_lint!()` invocations to follow the convention of upper-case
49+
/// statics without repeating the name.
50+
///
51+
/// The name is written with underscores, e.g. "unused_imports".
52+
/// On the command line, underscores become dashes.
4853
pub name: &'static str,
4954

5055
/// Default level for the lint.
@@ -56,6 +61,13 @@ pub struct Lint {
5661
pub desc: &'static str,
5762
}
5863

64+
impl Lint {
65+
/// Get the lint's name, with ASCII letters converted to lowercase.
66+
pub fn name_lower(&self) -> String {
67+
self.name.to_ascii_lower()
68+
}
69+
}
70+
5971
/// Build a `Lint` initializer.
6072
#[macro_export]
6173
macro_rules! lint_initializer (
@@ -186,8 +198,8 @@ impl LintId {
186198
}
187199

188200
/// Get the name of the lint.
189-
pub fn as_str(&self) -> &'static str {
190-
self.lint.name
201+
pub fn as_str(&self) -> String {
202+
self.lint.name_lower()
191203
}
192204
}
193205

0 commit comments

Comments
 (0)