Skip to content

Commit

Permalink
auto merge of #15773 : P1start/rust/style-lints, r=alexcrichton
Browse files Browse the repository at this point in the history
This unifies the `non_snake_case_functions` and `uppercase_variables` lints into one lint, `non_snake_case`. It also now checks for non-snake-case modules. This also extends the non-camel-case types lint to check type parameters, and merges the `non_uppercase_pattern_statics` lint into the `non_uppercase_statics` lint.

Because the `uppercase_variables` lint is now part of the `non_snake_case` lint, all non-snake-case variables that start with lowercase characters (such as `fooBar`) will now trigger the `non_snake_case` lint.

New code should be updated to use the new `non_snake_case` lint instead of the previous `non_snake_case_functions` and `uppercase_variables` lints. All use of the `non_uppercase_pattern_statics` should be replaced with the `non_uppercase_statics` lint. Any code that previously contained non-snake-case module or variable names should be updated to use snake case names or disable the `non_snake_case` lint. Any code with non-camel-case type parameters should be changed to use camel case or disable the `non_camel_case_types` lint.

This also adds support for lint groups to the compiler. Lint groups are a way of grouping a number of lints together under one name. For example, this also defines a default lint for naming conventions, named `bad_style`. Writing `#[allow(bad_style)]` is equivalent to writing `#[allow(non_camel_case_types, non_snake_case, non_uppercase_statics)]`. These lint groups can also be defined as a compiler plugin using the new `Registry::register_lint_group` method.

This also adds two built-in lint groups, `bad_style` and `unused`. The contents of these groups can be seen by running `rustc -W help`.

[breaking-change]
  • Loading branch information
bors committed Aug 29, 2014
2 parents bd159d3 + ed2aad8 commit 5419b2c
Show file tree
Hide file tree
Showing 64 changed files with 529 additions and 223 deletions.
2 changes: 1 addition & 1 deletion src/doc/guide-ffi.md
Expand Up @@ -477,7 +477,7 @@ extern crate libc;

#[cfg(target_os = "win32", target_arch = "x86")]
#[link(name = "kernel32")]
#[allow(non_snake_case_functions)]
#[allow(non_snake_case)]
extern "stdcall" {
fn SetEnvironmentVariableA(n: *const u8, v: *const u8) -> libc::c_int;
}
Expand Down
2 changes: 1 addition & 1 deletion src/etc/unicode.py
Expand Up @@ -34,7 +34,7 @@
// NOTE: The following code was generated by "src/etc/unicode.py", do not edit directly
#![allow(missing_doc, non_uppercase_statics, non_snake_case_functions)]
#![allow(missing_doc, non_uppercase_statics, non_snake_case)]
'''

# Mapping taken from Table 12 from:
Expand Down
2 changes: 1 addition & 1 deletion src/libcollections/hash/mod.rs
Expand Up @@ -157,8 +157,8 @@ macro_rules! impl_hash_tuple(

( $($name:ident)+) => (
impl<S: Writer, $($name: Hash<S>),*> Hash<S> for ($($name,)*) {
#[allow(uppercase_variables)]
#[inline]
#[allow(non_snake_case)]
fn hash(&self, state: &mut S) {
match *self {
($(ref $name,)*) => {
Expand Down
2 changes: 1 addition & 1 deletion src/libcore/char.rs
Expand Up @@ -12,7 +12,7 @@
//!
//! For more details, see ::unicode::char (a.k.a. std::char)

#![allow(non_snake_case_functions)]
#![allow(non_snake_case)]
#![doc(primitive = "char")]

use mem::transmute;
Expand Down
2 changes: 1 addition & 1 deletion src/libcore/fmt/mod.rs
Expand Up @@ -668,7 +668,7 @@ macro_rules! tuple (
() => ();
( $($name:ident,)+ ) => (
impl<$($name:Show),*> Show for ($($name,)*) {
#[allow(uppercase_variables, dead_assignment)]
#[allow(non_snake_case, dead_assignment)]
fn fmt(&self, f: &mut Formatter) -> Result {
try!(write!(f, "("));
let ($(ref $name,)*) = *self;
Expand Down
2 changes: 1 addition & 1 deletion src/libcore/ops.rs
Expand Up @@ -775,7 +775,7 @@ macro_rules! def_fn_mut(
FnMut<($($args,)*),Result>
for extern "Rust" fn($($args: $args,)*) -> Result {
#[rust_call_abi_hack]
#[allow(uppercase_variables)]
#[allow(non_snake_case)]
fn call_mut(&mut self, args: ($($args,)*)) -> Result {
let ($($args,)*) = args;
(*self)($($args,)*)
Expand Down
54 changes: 27 additions & 27 deletions src/libcore/str.rs
Expand Up @@ -394,9 +394,9 @@ impl NaiveSearcher {
fn next(&mut self, haystack: &[u8], needle: &[u8]) -> Option<(uint, uint)> {
while self.position + needle.len() <= haystack.len() {
if haystack.slice(self.position, self.position + needle.len()) == needle {
let matchPos = self.position;
let match_pos = self.position;
self.position += needle.len(); // add 1 for all matches
return Some((matchPos, matchPos + needle.len()));
return Some((match_pos, match_pos + needle.len()));
} else {
self.position += 1;
}
Expand All @@ -410,7 +410,7 @@ impl NaiveSearcher {
#[deriving(Clone)]
struct TwoWaySearcher {
// constants
critPos: uint,
crit_pos: uint,
period: uint,
byteset: u64,

Expand All @@ -423,32 +423,31 @@ struct TwoWaySearcher {
// Crochemore, M., Perrin, D., 1991, Two-way string-matching, Journal of the ACM 38(3):651-675.
impl TwoWaySearcher {
fn new(needle: &[u8]) -> TwoWaySearcher {
let (critPos1, period1) = TwoWaySearcher::maximal_suffix(needle, false);
let (critPos2, period2) = TwoWaySearcher::maximal_suffix(needle, true);
let (crit_pos1, period1) = TwoWaySearcher::maximal_suffix(needle, false);
let (crit_pos2, period2) = TwoWaySearcher::maximal_suffix(needle, true);

let critPos;
let crit_pos;
let period;
if critPos1 > critPos2 {
critPos = critPos1;
if crit_pos1 > crit_pos2 {
crit_pos = crit_pos1;
period = period1;
} else {
critPos = critPos2;
crit_pos = crit_pos2;
period = period2;
}

let byteset = needle.iter()
.fold(0, |a, &b| (1 << ((b & 0x3f) as uint)) | a);


// The logic here (calculating critPos and period, the final if statement to see which
// The logic here (calculating crit_pos and period, the final if statement to see which
// period to use for the TwoWaySearcher) is essentially an implementation of the
// "small-period" function from the paper (p. 670)
//
// In the paper they check whether `needle.slice_to(critPos)` is a suffix of
// `needle.slice(critPos, critPos + period)`, which is precisely what this does
if needle.slice_to(critPos) == needle.slice(period, period + critPos) {
// In the paper they check whether `needle.slice_to(crit_pos)` is a suffix of
// `needle.slice(crit_pos, crit_pos + period)`, which is precisely what this does
if needle.slice_to(crit_pos) == needle.slice(period, period + crit_pos) {
TwoWaySearcher {
critPos: critPos,
crit_pos: crit_pos,
period: period,
byteset: byteset,

Expand All @@ -457,8 +456,8 @@ impl TwoWaySearcher {
}
} else {
TwoWaySearcher {
critPos: critPos,
period: cmp::max(critPos, needle.len() - critPos) + 1,
crit_pos: crit_pos,
period: cmp::max(crit_pos, needle.len() - crit_pos) + 1,
byteset: byteset,

position: 0,
Expand All @@ -468,7 +467,7 @@ impl TwoWaySearcher {
}

#[inline]
fn next(&mut self, haystack: &[u8], needle: &[u8], longPeriod: bool) -> Option<(uint, uint)> {
fn next(&mut self, haystack: &[u8], needle: &[u8], long_period: bool) -> Option<(uint, uint)> {
'search: loop {
// Check that we have room to search in
if self.position + needle.len() > haystack.len() {
Expand All @@ -484,36 +483,37 @@ impl TwoWaySearcher {
}

// See if the right part of the needle matches
let start = if longPeriod { self.critPos } else { cmp::max(self.critPos, self.memory) };
let start = if long_period { self.crit_pos }
else { cmp::max(self.crit_pos, self.memory) };
for i in range(start, needle.len()) {
if needle[i] != haystack[self.position + i] {
self.position += i - self.critPos + 1;
if !longPeriod {
self.position += i - self.crit_pos + 1;
if !long_period {
self.memory = 0;
}
continue 'search;
}
}

// See if the left part of the needle matches
let start = if longPeriod { 0 } else { self.memory };
for i in range(start, self.critPos).rev() {
let start = if long_period { 0 } else { self.memory };
for i in range(start, self.crit_pos).rev() {
if needle[i] != haystack[self.position + i] {
self.position += self.period;
if !longPeriod {
if !long_period {
self.memory = needle.len() - self.period;
}
continue 'search;
}
}

// We have found a match!
let matchPos = self.position;
let match_pos = self.position;
self.position += needle.len(); // add self.period for all matches
if !longPeriod {
if !long_period {
self.memory = 0; // set to needle.len() - self.period for all matches
}
return Some((matchPos, matchPos + needle.len()));
return Some((match_pos, match_pos + needle.len()));
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/liblibc/lib.rs
Expand Up @@ -74,10 +74,10 @@
*/

#![allow(non_camel_case_types)]
#![allow(non_snake_case_functions)]
#![allow(non_snake_case)]
#![allow(non_uppercase_statics)]
#![allow(missing_doc)]
#![allow(uppercase_variables)]
#![allow(non_snake_case)]

#[cfg(test)] extern crate std;
#[cfg(test)] extern crate test;
Expand Down
2 changes: 1 addition & 1 deletion src/libnative/io/mod.rs
Expand Up @@ -21,7 +21,7 @@
//! play. The only dependencies of these modules are the normal system libraries
//! that you would find on the respective platform.

#![allow(non_snake_case_functions)]
#![allow(non_snake_case)]

use libc::c_int;
use libc;
Expand Down
2 changes: 1 addition & 1 deletion src/libnative/io/process.rs
Expand Up @@ -838,7 +838,7 @@ fn free_handle(_handle: *mut ()) {

#[cfg(unix)]
fn translate_status(status: c_int) -> rtio::ProcessExit {
#![allow(non_snake_case_functions)]
#![allow(non_snake_case)]
#[cfg(target_os = "linux")]
#[cfg(target_os = "android")]
mod imp {
Expand Down
1 change: 1 addition & 0 deletions src/libnum/bigint.rs
Expand Up @@ -78,6 +78,7 @@ pub type DoubleBigDigit = u64;
pub static ZERO_BIG_DIGIT: BigDigit = 0;
static ZERO_VEC: [BigDigit, ..1] = [ZERO_BIG_DIGIT];

#[allow(non_snake_case)]
pub mod BigDigit {
use super::BigDigit;
use super::DoubleBigDigit;
Expand Down
2 changes: 1 addition & 1 deletion src/librbml/lib.rs
Expand Up @@ -1132,7 +1132,7 @@ mod tests {

#[cfg(test)]
mod bench {
#![allow(non_snake_case_functions)]
#![allow(non_snake_case)]
use test::Bencher;
use super::reader;

Expand Down
2 changes: 1 addition & 1 deletion src/libregex/test/bench.rs
Expand Up @@ -7,7 +7,7 @@
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
#![allow(non_snake_case_functions)]
#![allow(non_snake_case)]

use std::rand::{Rng, task_rng};
use stdtest::Bencher;
Expand Down
2 changes: 2 additions & 0 deletions src/librustc/diagnostics.rs
Expand Up @@ -8,6 +8,8 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![allow(non_snake_case)]

register_diagnostic!(E0001, r##"
This error suggests that the expression arm corresponding to the noted pattern
will never be reached as for all possible values of the expression being matched,
Expand Down
6 changes: 5 additions & 1 deletion src/librustc/driver/driver.rs
Expand Up @@ -242,13 +242,17 @@ pub fn phase_2_configure_and_expand(sess: &Session,
}
});

let Registry { syntax_exts, lint_passes, .. } = registry;
let Registry { syntax_exts, lint_passes, lint_groups, .. } = registry;

{
let mut ls = sess.lint_store.borrow_mut();
for pass in lint_passes.move_iter() {
ls.register_pass(Some(sess), true, pass);
}

for (name, to) in lint_groups.move_iter() {
ls.register_group(Some(sess), true, name, to);
}
}

// Lint plugins are registered; now we can process command line flags.
Expand Down
68 changes: 56 additions & 12 deletions src/librustc/driver/mod.rs
Expand Up @@ -180,14 +180,26 @@ Available lint options:
lints
}

fn sort_lint_groups(lints: Vec<(&'static str, Vec<lint::LintId>, bool)>)
-> Vec<(&'static str, Vec<lint::LintId>)> {
let mut lints: Vec<_> = lints.move_iter().map(|(x, y, _)| (x, y)).collect();
lints.sort_by(|&(x, _): &(&'static str, Vec<lint::LintId>),
&(y, _): &(&'static str, Vec<lint::LintId>)| {
x.cmp(&y)
});
lints
}

let (plugin, builtin) = lint_store.get_lints().partitioned(|&(_, p)| p);
let plugin = sort_lints(plugin);
let builtin = sort_lints(builtin);

// FIXME (#7043): We should use the width in character cells rather than
// the number of codepoints.
let (plugin_groups, builtin_groups) = lint_store.get_lint_groups().partitioned(|&(_, _, p)| p);
let plugin_groups = sort_lint_groups(plugin_groups);
let builtin_groups = sort_lint_groups(builtin_groups);

let max_name_len = plugin.iter().chain(builtin.iter())
.map(|&s| s.name.char_len())
.map(|&s| s.name.width(true))
.max().unwrap_or(0);
let padded = |x: &str| {
" ".repeat(max_name_len - x.char_len()).append(x)
Expand All @@ -208,16 +220,48 @@ Available lint options:

print_lints(builtin);

match (loaded_plugins, plugin.len()) {
(false, 0) => {
println!("Compiler plugins can provide additional lints. To see a listing of these, \
re-run `rustc -W help` with a crate filename.");


let max_name_len = plugin_groups.iter().chain(builtin_groups.iter())
.map(|&(s, _)| s.width(true))
.max().unwrap_or(0);
let padded = |x: &str| {
" ".repeat(max_name_len - x.char_len()).append(x)
};

println!("Lint groups provided by rustc:\n");
println!(" {} {}", padded("name"), "sub-lints");
println!(" {} {}", padded("----"), "---------");

let print_lint_groups = |lints: Vec<(&'static str, Vec<lint::LintId>)>| {
for (name, to) in lints.move_iter() {
let name = name.chars().map(|x| x.to_lowercase())
.collect::<String>().replace("_", "-");
let desc = to.move_iter().map(|x| x.as_str()).collect::<Vec<String>>().connect(", ");
println!(" {} {}",
padded(name.as_slice()), desc);
}
(false, _) => fail!("didn't load lint plugins but got them anyway!"),
(true, 0) => println!("This crate does not load any lint plugins."),
(true, _) => {
println!("Lint checks provided by plugins loaded by this crate:\n");
print_lints(plugin);
println!("\n");
};

print_lint_groups(builtin_groups);

match (loaded_plugins, plugin.len(), plugin_groups.len()) {
(false, 0, _) | (false, _, 0) => {
println!("Compiler plugins can provide additional lints and lint groups. To see a \
listing of these, re-run `rustc -W help` with a crate filename.");
}
(false, _, _) => fail!("didn't load lint plugins but got them anyway!"),
(true, 0, 0) => println!("This crate does not load any lint plugins or lint groups."),
(true, l, g) => {
if l > 0 {
println!("Lint checks provided by plugins loaded by this crate:\n");
print_lints(plugin);
}
if g > 0 {
println!("Lint groups provided by plugins loaded by this crate:\n");
print_lint_groups(plugin_groups);
}
}
}
}
Expand Down

0 comments on commit 5419b2c

Please sign in to comment.