Skip to content

Commit c1c7659

Browse files
committed
update identifier naming warnings to give an example
This updates identifier warnings such as ``struct `foo_bar` should have a camel case identifier`` to provide an example. Closes #14738.
1 parent 01eb0ce commit c1c7659

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
@@ -1326,12 +1326,21 @@ fn check_item_non_camel_case_types(cx: &Context, it: &ast::Item) {
13261326
!ident.char_at(0).is_lowercase() && !ident.contains_char('_')
13271327
}
13281328

1329+
fn to_camel_case(s: &str) -> String {
1330+
s.split('_').flat_map(|word| word.chars().enumerate().map(|(i, c)|
1331+
if i == 0 { c.to_uppercase() }
1332+
else { c }
1333+
)).collect()
1334+
}
1335+
13291336
fn check_case(cx: &Context, sort: &str, ident: ast::Ident, span: Span) {
1337+
let s = token::get_ident(ident);
1338+
13301339
if !is_camel_case(ident) {
13311340
cx.span_lint(
13321341
NonCamelCaseTypes, span,
1333-
format!("{} `{}` should have a camel case identifier",
1334-
sort, token::get_ident(ident)).as_slice());
1342+
format!("{} `{}` should have a camel case name such as `{}`",
1343+
sort, s, to_camel_case(s.get())).as_slice());
13351344
}
13361345
}
13371346

@@ -1369,10 +1378,29 @@ fn check_snake_case(cx: &Context, sort: &str, ident: ast::Ident, span: Span) {
13691378
})
13701379
}
13711380

1381+
fn to_snake_case(str: &str) -> String {
1382+
let mut words = vec![];
1383+
for s in str.split('_') {
1384+
let mut buf = String::new();
1385+
if s.is_empty() { continue; }
1386+
for ch in s.chars() {
1387+
if !buf.is_empty() && ch.is_uppercase() {
1388+
words.push(buf);
1389+
buf = String::new();
1390+
}
1391+
buf.push_char(ch.to_lowercase());
1392+
}
1393+
words.push(buf);
1394+
}
1395+
words.connect("_")
1396+
}
1397+
1398+
let s = token::get_ident(ident);
1399+
13721400
if !is_snake_case(ident) {
13731401
cx.span_lint(NonSnakeCaseFunctions, span,
1374-
format!("{} `{}` should have a snake case identifier",
1375-
sort, token::get_ident(ident)).as_slice());
1402+
format!("{} `{}` should have a snake case name such as `{}`",
1403+
sort, s, to_snake_case(s.get())).as_slice());
13761404
}
13771405
}
13781406

@@ -1386,7 +1414,10 @@ fn check_item_non_uppercase_statics(cx: &Context, it: &ast::Item) {
13861414
// upper/lowercase)
13871415
if s.get().chars().any(|c| c.is_lowercase()) {
13881416
cx.span_lint(NonUppercaseStatics, it.span,
1389-
"static constant should have an uppercase identifier");
1417+
format!("static constant `{}` should have an uppercase name \
1418+
such as `{}`", s.get(),
1419+
s.get().chars().map(|c| c.to_uppercase())
1420+
.collect::<String>().as_slice()).as_slice());
13901421
}
13911422
}
13921423
_ => {}
@@ -1402,7 +1433,10 @@ fn check_pat_non_uppercase_statics(cx: &Context, p: &ast::Pat) {
14021433
let s = token::get_ident(ident);
14031434
if s.get().chars().any(|c| c.is_lowercase()) {
14041435
cx.span_lint(NonUppercasePatternStatics, path.span,
1405-
"static constant in pattern should be all caps");
1436+
format!("static constant in pattern `{}` should have an uppercase \
1437+
name such as `{}`", s.get(),
1438+
s.get().chars().map(|c| c.to_uppercase())
1439+
.collect::<String>().as_slice()).as_slice());
14061440
}
14071441
}
14081442
_ => {}

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)