Skip to content

Commit 5419b2c

Browse files
committed
auto merge of #15773 : P1start/rust/style-lints, r=alexcrichton
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]
2 parents bd159d3 + ed2aad8 commit 5419b2c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+529
-223
lines changed

src/doc/guide-ffi.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,7 @@ extern crate libc;
477477

478478
#[cfg(target_os = "win32", target_arch = "x86")]
479479
#[link(name = "kernel32")]
480-
#[allow(non_snake_case_functions)]
480+
#[allow(non_snake_case)]
481481
extern "stdcall" {
482482
fn SetEnvironmentVariableA(n: *const u8, v: *const u8) -> libc::c_int;
483483
}

src/etc/unicode.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
3535
// NOTE: The following code was generated by "src/etc/unicode.py", do not edit directly
3636
37-
#![allow(missing_doc, non_uppercase_statics, non_snake_case_functions)]
37+
#![allow(missing_doc, non_uppercase_statics, non_snake_case)]
3838
'''
3939

4040
# Mapping taken from Table 12 from:

src/libcollections/hash/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,8 @@ macro_rules! impl_hash_tuple(
157157

158158
( $($name:ident)+) => (
159159
impl<S: Writer, $($name: Hash<S>),*> Hash<S> for ($($name,)*) {
160-
#[allow(uppercase_variables)]
161160
#[inline]
161+
#[allow(non_snake_case)]
162162
fn hash(&self, state: &mut S) {
163163
match *self {
164164
($(ref $name,)*) => {

src/libcore/char.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
//!
1313
//! For more details, see ::unicode::char (a.k.a. std::char)
1414
15-
#![allow(non_snake_case_functions)]
15+
#![allow(non_snake_case)]
1616
#![doc(primitive = "char")]
1717

1818
use mem::transmute;

src/libcore/fmt/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -668,7 +668,7 @@ macro_rules! tuple (
668668
() => ();
669669
( $($name:ident,)+ ) => (
670670
impl<$($name:Show),*> Show for ($($name,)*) {
671-
#[allow(uppercase_variables, dead_assignment)]
671+
#[allow(non_snake_case, dead_assignment)]
672672
fn fmt(&self, f: &mut Formatter) -> Result {
673673
try!(write!(f, "("));
674674
let ($(ref $name,)*) = *self;

src/libcore/ops.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -775,7 +775,7 @@ macro_rules! def_fn_mut(
775775
FnMut<($($args,)*),Result>
776776
for extern "Rust" fn($($args: $args,)*) -> Result {
777777
#[rust_call_abi_hack]
778-
#[allow(uppercase_variables)]
778+
#[allow(non_snake_case)]
779779
fn call_mut(&mut self, args: ($($args,)*)) -> Result {
780780
let ($($args,)*) = args;
781781
(*self)($($args,)*)

src/libcore/str.rs

+27-27
Original file line numberDiff line numberDiff line change
@@ -394,9 +394,9 @@ impl NaiveSearcher {
394394
fn next(&mut self, haystack: &[u8], needle: &[u8]) -> Option<(uint, uint)> {
395395
while self.position + needle.len() <= haystack.len() {
396396
if haystack.slice(self.position, self.position + needle.len()) == needle {
397-
let matchPos = self.position;
397+
let match_pos = self.position;
398398
self.position += needle.len(); // add 1 for all matches
399-
return Some((matchPos, matchPos + needle.len()));
399+
return Some((match_pos, match_pos + needle.len()));
400400
} else {
401401
self.position += 1;
402402
}
@@ -410,7 +410,7 @@ impl NaiveSearcher {
410410
#[deriving(Clone)]
411411
struct TwoWaySearcher {
412412
// constants
413-
critPos: uint,
413+
crit_pos: uint,
414414
period: uint,
415415
byteset: u64,
416416

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

429-
let critPos;
429+
let crit_pos;
430430
let period;
431-
if critPos1 > critPos2 {
432-
critPos = critPos1;
431+
if crit_pos1 > crit_pos2 {
432+
crit_pos = crit_pos1;
433433
period = period1;
434434
} else {
435-
critPos = critPos2;
435+
crit_pos = crit_pos2;
436436
period = period2;
437437
}
438438

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

442-
443-
// The logic here (calculating critPos and period, the final if statement to see which
442+
// The logic here (calculating crit_pos and period, the final if statement to see which
444443
// period to use for the TwoWaySearcher) is essentially an implementation of the
445444
// "small-period" function from the paper (p. 670)
446445
//
447-
// In the paper they check whether `needle.slice_to(critPos)` is a suffix of
448-
// `needle.slice(critPos, critPos + period)`, which is precisely what this does
449-
if needle.slice_to(critPos) == needle.slice(period, period + critPos) {
446+
// In the paper they check whether `needle.slice_to(crit_pos)` is a suffix of
447+
// `needle.slice(crit_pos, crit_pos + period)`, which is precisely what this does
448+
if needle.slice_to(crit_pos) == needle.slice(period, period + crit_pos) {
450449
TwoWaySearcher {
451-
critPos: critPos,
450+
crit_pos: crit_pos,
452451
period: period,
453452
byteset: byteset,
454453

@@ -457,8 +456,8 @@ impl TwoWaySearcher {
457456
}
458457
} else {
459458
TwoWaySearcher {
460-
critPos: critPos,
461-
period: cmp::max(critPos, needle.len() - critPos) + 1,
459+
crit_pos: crit_pos,
460+
period: cmp::max(crit_pos, needle.len() - crit_pos) + 1,
462461
byteset: byteset,
463462

464463
position: 0,
@@ -468,7 +467,7 @@ impl TwoWaySearcher {
468467
}
469468

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

486485
// See if the right part of the needle matches
487-
let start = if longPeriod { self.critPos } else { cmp::max(self.critPos, self.memory) };
486+
let start = if long_period { self.crit_pos }
487+
else { cmp::max(self.crit_pos, self.memory) };
488488
for i in range(start, needle.len()) {
489489
if needle[i] != haystack[self.position + i] {
490-
self.position += i - self.critPos + 1;
491-
if !longPeriod {
490+
self.position += i - self.crit_pos + 1;
491+
if !long_period {
492492
self.memory = 0;
493493
}
494494
continue 'search;
495495
}
496496
}
497497

498498
// See if the left part of the needle matches
499-
let start = if longPeriod { 0 } else { self.memory };
500-
for i in range(start, self.critPos).rev() {
499+
let start = if long_period { 0 } else { self.memory };
500+
for i in range(start, self.crit_pos).rev() {
501501
if needle[i] != haystack[self.position + i] {
502502
self.position += self.period;
503-
if !longPeriod {
503+
if !long_period {
504504
self.memory = needle.len() - self.period;
505505
}
506506
continue 'search;
507507
}
508508
}
509509

510510
// We have found a match!
511-
let matchPos = self.position;
511+
let match_pos = self.position;
512512
self.position += needle.len(); // add self.period for all matches
513-
if !longPeriod {
513+
if !long_period {
514514
self.memory = 0; // set to needle.len() - self.period for all matches
515515
}
516-
return Some((matchPos, matchPos + needle.len()));
516+
return Some((match_pos, match_pos + needle.len()));
517517
}
518518
}
519519

src/liblibc/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,10 @@
7474
*/
7575

7676
#![allow(non_camel_case_types)]
77-
#![allow(non_snake_case_functions)]
77+
#![allow(non_snake_case)]
7878
#![allow(non_uppercase_statics)]
7979
#![allow(missing_doc)]
80-
#![allow(uppercase_variables)]
80+
#![allow(non_snake_case)]
8181

8282
#[cfg(test)] extern crate std;
8383
#[cfg(test)] extern crate test;

src/libnative/io/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
//! play. The only dependencies of these modules are the normal system libraries
2222
//! that you would find on the respective platform.
2323
24-
#![allow(non_snake_case_functions)]
24+
#![allow(non_snake_case)]
2525

2626
use libc::c_int;
2727
use libc;

src/libnative/io/process.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -838,7 +838,7 @@ fn free_handle(_handle: *mut ()) {
838838

839839
#[cfg(unix)]
840840
fn translate_status(status: c_int) -> rtio::ProcessExit {
841-
#![allow(non_snake_case_functions)]
841+
#![allow(non_snake_case)]
842842
#[cfg(target_os = "linux")]
843843
#[cfg(target_os = "android")]
844844
mod imp {

src/libnum/bigint.rs

+1
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ pub type DoubleBigDigit = u64;
7878
pub static ZERO_BIG_DIGIT: BigDigit = 0;
7979
static ZERO_VEC: [BigDigit, ..1] = [ZERO_BIG_DIGIT];
8080

81+
#[allow(non_snake_case)]
8182
pub mod BigDigit {
8283
use super::BigDigit;
8384
use super::DoubleBigDigit;

src/librbml/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1132,7 +1132,7 @@ mod tests {
11321132

11331133
#[cfg(test)]
11341134
mod bench {
1135-
#![allow(non_snake_case_functions)]
1135+
#![allow(non_snake_case)]
11361136
use test::Bencher;
11371137
use super::reader;
11381138

src/libregex/test/bench.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
10-
#![allow(non_snake_case_functions)]
10+
#![allow(non_snake_case)]
1111

1212
use std::rand::{Rng, task_rng};
1313
use stdtest::Bencher;

src/librustc/diagnostics.rs

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
#![allow(non_snake_case)]
12+
1113
register_diagnostic!(E0001, r##"
1214
This error suggests that the expression arm corresponding to the noted pattern
1315
will never be reached as for all possible values of the expression being matched,

src/librustc/driver/driver.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -242,13 +242,17 @@ pub fn phase_2_configure_and_expand(sess: &Session,
242242
}
243243
});
244244

245-
let Registry { syntax_exts, lint_passes, .. } = registry;
245+
let Registry { syntax_exts, lint_passes, lint_groups, .. } = registry;
246246

247247
{
248248
let mut ls = sess.lint_store.borrow_mut();
249249
for pass in lint_passes.move_iter() {
250250
ls.register_pass(Some(sess), true, pass);
251251
}
252+
253+
for (name, to) in lint_groups.move_iter() {
254+
ls.register_group(Some(sess), true, name, to);
255+
}
252256
}
253257

254258
// Lint plugins are registered; now we can process command line flags.

src/librustc/driver/mod.rs

+56-12
Original file line numberDiff line numberDiff line change
@@ -180,14 +180,26 @@ Available lint options:
180180
lints
181181
}
182182

183+
fn sort_lint_groups(lints: Vec<(&'static str, Vec<lint::LintId>, bool)>)
184+
-> Vec<(&'static str, Vec<lint::LintId>)> {
185+
let mut lints: Vec<_> = lints.move_iter().map(|(x, y, _)| (x, y)).collect();
186+
lints.sort_by(|&(x, _): &(&'static str, Vec<lint::LintId>),
187+
&(y, _): &(&'static str, Vec<lint::LintId>)| {
188+
x.cmp(&y)
189+
});
190+
lints
191+
}
192+
183193
let (plugin, builtin) = lint_store.get_lints().partitioned(|&(_, p)| p);
184194
let plugin = sort_lints(plugin);
185195
let builtin = sort_lints(builtin);
186196

187-
// FIXME (#7043): We should use the width in character cells rather than
188-
// the number of codepoints.
197+
let (plugin_groups, builtin_groups) = lint_store.get_lint_groups().partitioned(|&(_, _, p)| p);
198+
let plugin_groups = sort_lint_groups(plugin_groups);
199+
let builtin_groups = sort_lint_groups(builtin_groups);
200+
189201
let max_name_len = plugin.iter().chain(builtin.iter())
190-
.map(|&s| s.name.char_len())
202+
.map(|&s| s.name.width(true))
191203
.max().unwrap_or(0);
192204
let padded = |x: &str| {
193205
" ".repeat(max_name_len - x.char_len()).append(x)
@@ -208,16 +220,48 @@ Available lint options:
208220

209221
print_lints(builtin);
210222

211-
match (loaded_plugins, plugin.len()) {
212-
(false, 0) => {
213-
println!("Compiler plugins can provide additional lints. To see a listing of these, \
214-
re-run `rustc -W help` with a crate filename.");
223+
224+
225+
let max_name_len = plugin_groups.iter().chain(builtin_groups.iter())
226+
.map(|&(s, _)| s.width(true))
227+
.max().unwrap_or(0);
228+
let padded = |x: &str| {
229+
" ".repeat(max_name_len - x.char_len()).append(x)
230+
};
231+
232+
println!("Lint groups provided by rustc:\n");
233+
println!(" {} {}", padded("name"), "sub-lints");
234+
println!(" {} {}", padded("----"), "---------");
235+
236+
let print_lint_groups = |lints: Vec<(&'static str, Vec<lint::LintId>)>| {
237+
for (name, to) in lints.move_iter() {
238+
let name = name.chars().map(|x| x.to_lowercase())
239+
.collect::<String>().replace("_", "-");
240+
let desc = to.move_iter().map(|x| x.as_str()).collect::<Vec<String>>().connect(", ");
241+
println!(" {} {}",
242+
padded(name.as_slice()), desc);
215243
}
216-
(false, _) => fail!("didn't load lint plugins but got them anyway!"),
217-
(true, 0) => println!("This crate does not load any lint plugins."),
218-
(true, _) => {
219-
println!("Lint checks provided by plugins loaded by this crate:\n");
220-
print_lints(plugin);
244+
println!("\n");
245+
};
246+
247+
print_lint_groups(builtin_groups);
248+
249+
match (loaded_plugins, plugin.len(), plugin_groups.len()) {
250+
(false, 0, _) | (false, _, 0) => {
251+
println!("Compiler plugins can provide additional lints and lint groups. To see a \
252+
listing of these, re-run `rustc -W help` with a crate filename.");
253+
}
254+
(false, _, _) => fail!("didn't load lint plugins but got them anyway!"),
255+
(true, 0, 0) => println!("This crate does not load any lint plugins or lint groups."),
256+
(true, l, g) => {
257+
if l > 0 {
258+
println!("Lint checks provided by plugins loaded by this crate:\n");
259+
print_lints(plugin);
260+
}
261+
if g > 0 {
262+
println!("Lint groups provided by plugins loaded by this crate:\n");
263+
print_lint_groups(plugin_groups);
264+
}
221265
}
222266
}
223267
}

0 commit comments

Comments
 (0)