Skip to content

Commit

Permalink
Use names in Lint structs in an ASCII-case-insensitive way
Browse files Browse the repository at this point in the history
In preparation for the next commit.
  • Loading branch information
Keegan McAllister committed Jun 24, 2014
1 parent 609552e commit 21e7b93
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 13 deletions.
3 changes: 2 additions & 1 deletion src/librustc/driver/mod.rs
Expand Up @@ -146,6 +146,7 @@ Available lint options:
let mut lints: Vec<_> = lints.move_iter().map(|(x, _)| x).collect();
lints.sort_by(|x: &&Lint, y: &&Lint| {
match x.default_level.cmp(&y.default_level) {
// The sort doesn't case-fold but it's doubtful we care.
Equal => x.name.cmp(&y.name),
r => r,
}
Expand All @@ -172,7 +173,7 @@ Available lint options:

let print_lints = |lints: Vec<&Lint>| {
for lint in lints.move_iter() {
let name = lint.name.replace("_", "-");
let name = lint.name_lower().replace("_", "-");
println!(" {} {:7.7s} {}",
padded(name.as_slice()), lint.default_level.as_str(), lint.desc);
}
Expand Down
13 changes: 7 additions & 6 deletions src/librustc/lint/context.rs
Expand Up @@ -61,7 +61,7 @@ pub struct LintStore {
passes: Option<Vec<LintPassObject>>,

/// Lints indexed by name.
by_name: HashMap<&'static str, LintId>,
by_name: HashMap<String, LintId>,

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

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

let name = lint.name_lower();
let mut note = None;
let msg = match source {
Default => {
format!("{}, #[{}({})] on by default", msg,
level.as_str(), lint.name)
level.as_str(), name)
},
CommandLine => {
format!("{} [-{} {}]", msg,
match level {
Warn => 'W', Deny => 'D', Forbid => 'F',
Allow => fail!()
}, lint.name.replace("_", "-"))
}, name.replace("_", "-"))
},
Node(src) => {
note = Some(src);
Expand Down Expand Up @@ -355,7 +356,7 @@ impl<'a> Context<'a> {
for meta in metas.iter() {
match meta.node {
ast::MetaWord(ref lint_name) => {
match self.lints.by_name.find_equiv(lint_name) {
match self.lints.by_name.find_equiv(&lint_name.get()) {
Some(lint_id) => out.push((*lint_id, level, meta.span)),

None => self.span_lint(builtin::unrecognized_lint,
Expand Down
24 changes: 18 additions & 6 deletions src/librustc/lint/mod.rs
Expand Up @@ -31,6 +31,7 @@

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

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

impl Lint {
/// Get the lint's name, with ASCII letters converted to lowercase.
pub fn name_lower(&self) -> String {
self.name.to_ascii_lower()
}
}

/// Build a `Lint` initializer.
#[macro_export]
macro_rules! lint_initializer (
Expand Down Expand Up @@ -186,8 +198,8 @@ impl LintId {
}

/// Get the name of the lint.
pub fn as_str(&self) -> &'static str {
self.lint.name
pub fn as_str(&self) -> String {
self.lint.name_lower()
}
}

Expand Down

0 comments on commit 21e7b93

Please sign in to comment.