Skip to content

Commit 1b28f5a

Browse files
committed
improve non_camel_case_types diagnostics
Use a structured suggestion and tighten the span to just the identifier.
1 parent e379970 commit 1b28f5a

9 files changed

+83
-82
lines changed

src/librustc_lint/nonstandard_style.rs

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,12 @@ declare_lint! {
4141
pub struct NonCamelCaseTypes;
4242

4343
impl NonCamelCaseTypes {
44-
fn check_case(&self, cx: &EarlyContext, sort: &str, name: ast::Name, span: Span) {
44+
fn check_case(&self, cx: &EarlyContext, sort: &str, ident: &Ident) {
4545
fn char_has_case(c: char) -> bool {
4646
c.is_lowercase() || c.is_uppercase()
4747
}
4848

49-
fn is_camel_case(name: ast::Name) -> bool {
50-
let name = name.as_str();
49+
fn is_camel_case(name: &str) -> bool {
5150
let name = name.trim_matches('_');
5251
if name.is_empty() {
5352
return true;
@@ -87,14 +86,20 @@ impl NonCamelCaseTypes {
8786
}).0
8887
}
8988

89+
let name = &ident.name.as_str();
90+
9091
if !is_camel_case(name) {
91-
let c = to_camel_case(&name.as_str());
92-
let m = if c.is_empty() {
93-
format!("{} `{}` should have a camel case name such as `CamelCase`", sort, name)
94-
} else {
95-
format!("{} `{}` should have a camel case name such as `{}`", sort, name, c)
96-
};
97-
cx.span_lint(NON_CAMEL_CASE_TYPES, span, &m);
92+
let c = to_camel_case(name);
93+
94+
let msg = format!("{} `{}` should have a camel case name", sort, name);
95+
cx.struct_span_lint(NON_CAMEL_CASE_TYPES, ident.span, &msg)
96+
.span_suggestion_with_applicability(
97+
ident.span,
98+
"convert the identifier to camel case",
99+
c,
100+
Applicability::MaybeIncorrect,
101+
)
102+
.emit();
98103
}
99104
}
100105
}
@@ -123,19 +128,19 @@ impl EarlyLintPass for NonCamelCaseTypes {
123128
ast::ItemKind::Ty(..) |
124129
ast::ItemKind::Enum(..) |
125130
ast::ItemKind::Struct(..) |
126-
ast::ItemKind::Union(..) => self.check_case(cx, "type", it.ident.name, it.span),
127-
ast::ItemKind::Trait(..) => self.check_case(cx, "trait", it.ident.name, it.span),
131+
ast::ItemKind::Union(..) => self.check_case(cx, "type", &it.ident),
132+
ast::ItemKind::Trait(..) => self.check_case(cx, "trait", &it.ident),
128133
_ => (),
129134
}
130135
}
131136

132137
fn check_variant(&mut self, cx: &EarlyContext, v: &ast::Variant, _: &ast::Generics) {
133-
self.check_case(cx, "variant", v.node.ident.name, v.span);
138+
self.check_case(cx, "variant", &v.node.ident);
134139
}
135140

136141
fn check_generic_param(&mut self, cx: &EarlyContext, param: &ast::GenericParam) {
137142
if let ast::GenericParamKind::Type { .. } = param.kind {
138-
self.check_case(cx, "type parameter", param.ident.name, param.ident.span);
143+
self.check_case(cx, "type parameter", &param.ident);
139144
}
140145
}
141146
}

src/test/ui/lint/lint-group-nonstandard-style.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
warning: type `snake_case` should have a camel case name such as `SnakeCase`
2-
--> $DIR/lint-group-nonstandard-style.rs:22:9
1+
warning: type `snake_case` should have a camel case name
2+
--> $DIR/lint-group-nonstandard-style.rs:22:16
33
|
44
LL | struct snake_case; //~ WARN should have a camel
5-
| ^^^^^^^^^^^^^^^^^^
5+
| ^^^^^^^^^^ help: convert the identifier to camel case: `SnakeCase`
66
|
77
note: lint level defined here
88
--> $DIR/lint-group-nonstandard-style.rs:18:17

src/test/ui/lint/lint-non-camel-case-types.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,31 @@
22
#![allow(dead_code)]
33

44
struct ONE_TWO_THREE;
5-
//~^ ERROR type `ONE_TWO_THREE` should have a camel case name such as `OneTwoThree`
5+
//~^ ERROR type `ONE_TWO_THREE` should have a camel case name
66

7-
struct foo { //~ ERROR type `foo` should have a camel case name such as `Foo`
7+
struct foo { //~ ERROR type `foo` should have a camel case name
88
bar: isize,
99
}
1010

11-
enum foo2 { //~ ERROR type `foo2` should have a camel case name such as `Foo2`
11+
enum foo2 { //~ ERROR type `foo2` should have a camel case name
1212
Bar
1313
}
1414

15-
struct foo3 { //~ ERROR type `foo3` should have a camel case name such as `Foo3`
15+
struct foo3 { //~ ERROR type `foo3` should have a camel case name
1616
bar: isize
1717
}
1818

19-
type foo4 = isize; //~ ERROR type `foo4` should have a camel case name such as `Foo4`
19+
type foo4 = isize; //~ ERROR type `foo4` should have a camel case name
2020

2121
enum Foo5 {
22-
bar //~ ERROR variant `bar` should have a camel case name such as `Bar`
22+
bar //~ ERROR variant `bar` should have a camel case name
2323
}
2424

25-
trait foo6 { //~ ERROR trait `foo6` should have a camel case name such as `Foo6`
25+
trait foo6 { //~ ERROR trait `foo6` should have a camel case name
2626
fn dummy(&self) { }
2727
}
2828

29-
fn f<ty>(_: ty) {} //~ ERROR type parameter `ty` should have a camel case name such as `Ty`
29+
fn f<ty>(_: ty) {} //~ ERROR type parameter `ty` should have a camel case name
3030

3131
#[repr(C)]
3232
struct foo7 {
@@ -35,10 +35,10 @@ struct foo7 {
3535

3636
struct X86_64;
3737

38-
struct X86__64; //~ ERROR type `X86__64` should have a camel case name such as `X86_64`
38+
struct X86__64; //~ ERROR type `X86__64` should have a camel case name
3939

40-
struct Abc_123; //~ ERROR type `Abc_123` should have a camel case name such as `Abc123`
40+
struct Abc_123; //~ ERROR type `Abc_123` should have a camel case name
4141

42-
struct A1_b2_c3; //~ ERROR type `A1_b2_c3` should have a camel case name such as `A1B2C3`
42+
struct A1_b2_c3; //~ ERROR type `A1_b2_c3` should have a camel case name
4343

4444
fn main() { }
Lines changed: 41 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,74 @@
1-
error: type `ONE_TWO_THREE` should have a camel case name such as `OneTwoThree`
2-
--> $DIR/lint-non-camel-case-types.rs:4:1
1+
error: type `ONE_TWO_THREE` should have a camel case name
2+
--> $DIR/lint-non-camel-case-types.rs:4:8
33
|
44
LL | struct ONE_TWO_THREE;
5-
| ^^^^^^^^^^^^^^^^^^^^^
5+
| ^^^^^^^^^^^^^ help: convert the identifier to camel case: `OneTwoThree`
66
|
77
note: lint level defined here
88
--> $DIR/lint-non-camel-case-types.rs:1:11
99
|
1010
LL | #![forbid(non_camel_case_types)]
1111
| ^^^^^^^^^^^^^^^^^^^^
1212

13-
error: type `foo` should have a camel case name such as `Foo`
14-
--> $DIR/lint-non-camel-case-types.rs:7:1
13+
error: type `foo` should have a camel case name
14+
--> $DIR/lint-non-camel-case-types.rs:7:8
1515
|
16-
LL | / struct foo { //~ ERROR type `foo` should have a camel case name such as `Foo`
17-
LL | | bar: isize,
18-
LL | | }
19-
| |_^
16+
LL | struct foo { //~ ERROR type `foo` should have a camel case name
17+
| ^^^ help: convert the identifier to camel case: `Foo`
2018

21-
error: type `foo2` should have a camel case name such as `Foo2`
22-
--> $DIR/lint-non-camel-case-types.rs:11:1
19+
error: type `foo2` should have a camel case name
20+
--> $DIR/lint-non-camel-case-types.rs:11:6
2321
|
24-
LL | / enum foo2 { //~ ERROR type `foo2` should have a camel case name such as `Foo2`
25-
LL | | Bar
26-
LL | | }
27-
| |_^
22+
LL | enum foo2 { //~ ERROR type `foo2` should have a camel case name
23+
| ^^^^ help: convert the identifier to camel case: `Foo2`
2824

29-
error: type `foo3` should have a camel case name such as `Foo3`
30-
--> $DIR/lint-non-camel-case-types.rs:15:1
25+
error: type `foo3` should have a camel case name
26+
--> $DIR/lint-non-camel-case-types.rs:15:8
3127
|
32-
LL | / struct foo3 { //~ ERROR type `foo3` should have a camel case name such as `Foo3`
33-
LL | | bar: isize
34-
LL | | }
35-
| |_^
28+
LL | struct foo3 { //~ ERROR type `foo3` should have a camel case name
29+
| ^^^^ help: convert the identifier to camel case: `Foo3`
3630

37-
error: type `foo4` should have a camel case name such as `Foo4`
38-
--> $DIR/lint-non-camel-case-types.rs:19:1
31+
error: type `foo4` should have a camel case name
32+
--> $DIR/lint-non-camel-case-types.rs:19:6
3933
|
40-
LL | type foo4 = isize; //~ ERROR type `foo4` should have a camel case name such as `Foo4`
41-
| ^^^^^^^^^^^^^^^^^^
34+
LL | type foo4 = isize; //~ ERROR type `foo4` should have a camel case name
35+
| ^^^^ help: convert the identifier to camel case: `Foo4`
4236

43-
error: variant `bar` should have a camel case name such as `Bar`
37+
error: variant `bar` should have a camel case name
4438
--> $DIR/lint-non-camel-case-types.rs:22:5
4539
|
46-
LL | bar //~ ERROR variant `bar` should have a camel case name such as `Bar`
47-
| ^^^
40+
LL | bar //~ ERROR variant `bar` should have a camel case name
41+
| ^^^ help: convert the identifier to camel case: `Bar`
4842

49-
error: trait `foo6` should have a camel case name such as `Foo6`
50-
--> $DIR/lint-non-camel-case-types.rs:25:1
43+
error: trait `foo6` should have a camel case name
44+
--> $DIR/lint-non-camel-case-types.rs:25:7
5145
|
52-
LL | / trait foo6 { //~ ERROR trait `foo6` should have a camel case name such as `Foo6`
53-
LL | | fn dummy(&self) { }
54-
LL | | }
55-
| |_^
46+
LL | trait foo6 { //~ ERROR trait `foo6` should have a camel case name
47+
| ^^^^ help: convert the identifier to camel case: `Foo6`
5648

57-
error: type parameter `ty` should have a camel case name such as `Ty`
49+
error: type parameter `ty` should have a camel case name
5850
--> $DIR/lint-non-camel-case-types.rs:29:6
5951
|
60-
LL | fn f<ty>(_: ty) {} //~ ERROR type parameter `ty` should have a camel case name such as `Ty`
61-
| ^^
52+
LL | fn f<ty>(_: ty) {} //~ ERROR type parameter `ty` should have a camel case name
53+
| ^^ help: convert the identifier to camel case: `Ty`
6254

63-
error: type `X86__64` should have a camel case name such as `X86_64`
64-
--> $DIR/lint-non-camel-case-types.rs:38:1
55+
error: type `X86__64` should have a camel case name
56+
--> $DIR/lint-non-camel-case-types.rs:38:8
6557
|
66-
LL | struct X86__64; //~ ERROR type `X86__64` should have a camel case name such as `X86_64`
67-
| ^^^^^^^^^^^^^^^
58+
LL | struct X86__64; //~ ERROR type `X86__64` should have a camel case name
59+
| ^^^^^^^ help: convert the identifier to camel case: `X86_64`
6860

69-
error: type `Abc_123` should have a camel case name such as `Abc123`
70-
--> $DIR/lint-non-camel-case-types.rs:40:1
61+
error: type `Abc_123` should have a camel case name
62+
--> $DIR/lint-non-camel-case-types.rs:40:8
7163
|
72-
LL | struct Abc_123; //~ ERROR type `Abc_123` should have a camel case name such as `Abc123`
73-
| ^^^^^^^^^^^^^^^
64+
LL | struct Abc_123; //~ ERROR type `Abc_123` should have a camel case name
65+
| ^^^^^^^ help: convert the identifier to camel case: `Abc123`
7466

75-
error: type `A1_b2_c3` should have a camel case name such as `A1B2C3`
76-
--> $DIR/lint-non-camel-case-types.rs:42:1
67+
error: type `A1_b2_c3` should have a camel case name
68+
--> $DIR/lint-non-camel-case-types.rs:42:8
7769
|
78-
LL | struct A1_b2_c3; //~ ERROR type `A1_b2_c3` should have a camel case name such as `A1B2C3`
79-
| ^^^^^^^^^^^^^^^^
70+
LL | struct A1_b2_c3; //~ ERROR type `A1_b2_c3` should have a camel case name
71+
| ^^^^^^^^ help: convert the identifier to camel case: `A1B2C3`
8072

8173
error: aborting due to 11 previous errors
8274

src/test/run-pass/test-allow-non-camel-case-variant.rs renamed to src/test/ui/lint/lint-non-camel-case-variant.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// compile-pass
2+
13
#![deny(non_camel_case_types)]
24

35
pub enum Foo {

src/test/run-pass/lint-non-camel-case-with-trailing-underscores.rs renamed to src/test/ui/lint/lint-non-camel-case-with-trailing-underscores.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// compile-pass
2+
13
#![allow(dead_code)]
24
// This is ok because we often use the trailing underscore to mean 'prime'
35

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
//
2-
#![allow(dead_code)]
1+
// compile-pass
32

3+
#![allow(dead_code)]
44

55
#![forbid(non_camel_case_types)]
66
#![forbid(non_upper_case_globals)]

src/test/ui/utf8_idents.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
fn foo<
44
'β, //~ ERROR non-ascii idents are not fully supported
55
γ //~ ERROR non-ascii idents are not fully supported
6-
//~^ WARN type parameter `γ` should have a camel case name such as `Γ`
6+
//~^ WARN type parameter `γ` should have a camel case name
77
>() {}
88

99
struct X {

src/test/ui/utf8_idents.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ LL | let α = 0.00001f64; //~ ERROR non-ascii idents are not fully supported
3030
|
3131
= help: add #![feature(non_ascii_idents)] to the crate attributes to enable
3232

33-
warning: type parameter `γ` should have a camel case name such as `Γ`
33+
warning: type parameter `γ` should have a camel case name
3434
--> $DIR/utf8_idents.rs:5:5
3535
|
3636
LL | γ //~ ERROR non-ascii idents are not fully supported
37-
| ^
37+
| ^ help: convert the identifier to camel case: `Γ`
3838
|
3939
= note: #[warn(non_camel_case_types)] on by default
4040

0 commit comments

Comments
 (0)