Skip to content

Commit a6a9e09

Browse files
committed
auto merge of #14740 : P1start/rust/name-warnings, r=alexcrichton
This updates identifier warnings such as ``struct `foo_bar` should have a camel case identifier`` to provide an example. Closes #14738.
2 parents 61d65cd + c1c7659 commit a6a9e09

File tree

5 files changed

+58
-24
lines changed

5 files changed

+58
-24
lines changed

src/librustc/middle/lint.rs

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1253,12 +1253,21 @@ fn check_item_non_camel_case_types(cx: &Context, it: &ast::Item) {
12531253
!ident.char_at(0).is_lowercase() && !ident.contains_char('_')
12541254
}
12551255

1256+
fn to_camel_case(s: &str) -> String {
1257+
s.split('_').flat_map(|word| word.chars().enumerate().map(|(i, c)|
1258+
if i == 0 { c.to_uppercase() }
1259+
else { c }
1260+
)).collect()
1261+
}
1262+
12561263
fn check_case(cx: &Context, sort: &str, ident: ast::Ident, span: Span) {
1264+
let s = token::get_ident(ident);
1265+
12571266
if !is_camel_case(ident) {
12581267
cx.span_lint(
12591268
NonCamelCaseTypes, span,
1260-
format!("{} `{}` should have a camel case identifier",
1261-
sort, token::get_ident(ident)).as_slice());
1269+
format!("{} `{}` should have a camel case name such as `{}`",
1270+
sort, s, to_camel_case(s.get())).as_slice());
12621271
}
12631272
}
12641273

@@ -1296,10 +1305,29 @@ fn check_snake_case(cx: &Context, sort: &str, ident: ast::Ident, span: Span) {
12961305
})
12971306
}
12981307

1308+
fn to_snake_case(str: &str) -> String {
1309+
let mut words = vec![];
1310+
for s in str.split('_') {
1311+
let mut buf = String::new();
1312+
if s.is_empty() { continue; }
1313+
for ch in s.chars() {
1314+
if !buf.is_empty() && ch.is_uppercase() {
1315+
words.push(buf);
1316+
buf = String::new();
1317+
}
1318+
buf.push_char(ch.to_lowercase());
1319+
}
1320+
words.push(buf);
1321+
}
1322+
words.connect("_")
1323+
}
1324+
1325+
let s = token::get_ident(ident);
1326+
12991327
if !is_snake_case(ident) {
13001328
cx.span_lint(NonSnakeCaseFunctions, span,
1301-
format!("{} `{}` should have a snake case identifier",
1302-
sort, token::get_ident(ident)).as_slice());
1329+
format!("{} `{}` should have a snake case name such as `{}`",
1330+
sort, s, to_snake_case(s.get())).as_slice());
13031331
}
13041332
}
13051333

@@ -1313,7 +1341,10 @@ fn check_item_non_uppercase_statics(cx: &Context, it: &ast::Item) {
13131341
// upper/lowercase)
13141342
if s.get().chars().any(|c| c.is_lowercase()) {
13151343
cx.span_lint(NonUppercaseStatics, it.span,
1316-
"static constant should have an uppercase identifier");
1344+
format!("static constant `{}` should have an uppercase name \
1345+
such as `{}`", s.get(),
1346+
s.get().chars().map(|c| c.to_uppercase())
1347+
.collect::<String>().as_slice()).as_slice());
13171348
}
13181349
}
13191350
_ => {}
@@ -1329,7 +1360,10 @@ fn check_pat_non_uppercase_statics(cx: &Context, p: &ast::Pat) {
13291360
let s = token::get_ident(ident);
13301361
if s.get().chars().any(|c| c.is_lowercase()) {
13311362
cx.span_lint(NonUppercasePatternStatics, path.span,
1332-
"static constant in pattern should be all caps");
1363+
format!("static constant in pattern `{}` should have an uppercase \
1364+
name such as `{}`", s.get(),
1365+
s.get().chars().map(|c| c.to_uppercase())
1366+
.collect::<String>().as_slice()).as_slice());
13331367
}
13341368
}
13351369
_ => {}

src/test/compile-fail/lint-non-camel-case-types.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,25 @@
1111
#![forbid(non_camel_case_types)]
1212
#![allow(dead_code)]
1313

14-
struct foo { //~ ERROR type `foo` should have a camel case identifier
14+
struct foo { //~ ERROR type `foo` should have a camel case name such as `Foo`
1515
bar: int,
1616
}
1717

18-
enum foo2 { //~ ERROR type `foo2` should have a camel case identifier
18+
enum foo2 { //~ ERROR type `foo2` should have a camel case name such as `Foo2`
1919
Bar
2020
}
2121

22-
struct foo3 { //~ ERROR type `foo3` should have a camel case identifier
22+
struct foo3 { //~ ERROR type `foo3` should have a camel case name such as `Foo3`
2323
bar: int
2424
}
2525

26-
type foo4 = int; //~ ERROR type `foo4` should have a camel case identifier
26+
type foo4 = int; //~ ERROR type `foo4` should have a camel case name such as `Foo4`
2727

2828
enum Foo5 {
29-
bar //~ ERROR variant `bar` should have a camel case identifier
29+
bar //~ ERROR variant `bar` should have a camel case name such as `Bar`
3030
}
3131

32-
trait foo6 { //~ ERROR trait `foo6` should have a camel case identifier
32+
trait foo6 { //~ ERROR trait `foo6` should have a camel case name such as `Foo6`
3333
}
3434

3535
fn main() { }

src/test/compile-fail/lint-non-snake-case-functions.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,25 @@ struct Foo;
1515

1616
impl Foo {
1717
fn Foo_Method() {}
18-
//~^ ERROR method `Foo_Method` should have a snake case identifier
18+
//~^ ERROR method `Foo_Method` should have a snake case name such as `foo_method`
1919

2020
// Don't allow two underscores in a row
2121
fn foo__method(&self) {}
22-
//~^ ERROR method `foo__method` should have a snake case identifier
22+
//~^ ERROR method `foo__method` should have a snake case name such as `foo_method`
2323

2424
pub fn xyZ(&mut self) {}
25-
//~^ ERROR method `xyZ` should have a snake case identifier
25+
//~^ ERROR method `xyZ` should have a snake case name such as `xy_z`
2626
}
2727

2828
trait X {
2929
fn ABC();
30-
//~^ ERROR trait method `ABC` should have a snake case identifier
30+
//~^ ERROR trait method `ABC` should have a snake case name such as `a_b_c`
3131

3232
fn a_b_C(&self) {}
33-
//~^ ERROR trait method `a_b_C` should have a snake case identifier
33+
//~^ ERROR trait method `a_b_C` should have a snake case name such as `a_b_c`
3434

3535
fn something__else(&mut self);
36-
//~^ ERROR trait method `something__else` should have a snake case identifier
36+
//~^ ERROR trait method `something__else` should have a snake case name such as `something_else`
3737
}
3838

3939
impl X for Foo {
@@ -43,9 +43,9 @@ impl X for Foo {
4343
}
4444

4545
fn Cookie() {}
46-
//~^ ERROR function `Cookie` should have a snake case identifier
46+
//~^ ERROR function `Cookie` should have a snake case name such as `cookie`
4747

4848
pub fn bi_S_Cuit() {}
49-
//~^ ERROR function `bi_S_Cuit` should have a snake case identifier
49+
//~^ ERROR function `bi_S_Cuit` should have a snake case name such as `bi_s_cuit`
5050

5151
fn main() { }

src/test/compile-fail/lint-non-uppercase-statics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@
1111
#![forbid(non_uppercase_statics)]
1212
#![allow(dead_code)]
1313

14-
static foo: int = 1; //~ ERROR static constant should have an uppercase identifier
14+
static foo: int = 1; //~ ERROR static constant `foo` should have an uppercase name such as `FOO`
1515

1616
fn main() { }

src/test/compile-fail/match-static-const-lc.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pub static a : int = 97;
1818
fn f() {
1919
let r = match (0,0) {
2020
(0, a) => 0,
21-
//~^ ERROR static constant in pattern should be all caps
21+
//~^ ERROR static constant in pattern `a` should have an uppercase name such as `A`
2222
(x, y) => 1 + x + y,
2323
};
2424
assert!(r == 1);
@@ -32,7 +32,7 @@ fn g() {
3232
use self::m::aha;
3333
let r = match (0,0) {
3434
(0, aha) => 0,
35-
//~^ ERROR static constant in pattern should be all caps
35+
//~^ ERROR static constant in pattern `aha` should have an uppercase name such as `AHA`
3636
(x, y) => 1 + x + y,
3737
};
3838
assert!(r == 1);
@@ -46,7 +46,7 @@ fn h() {
4646
use not_okay = self::n::OKAY;
4747
let r = match (0,0) {
4848
(0, not_okay) => 0,
49-
//~^ ERROR static constant in pattern should be all caps
49+
//~^ ERROR static constant in pattern `not_okay` should have an uppercase name such as `NOT_OKAY`
5050
(x, y) => 1 + x + y,
5151
};
5252
assert!(r == 1);

0 commit comments

Comments
 (0)