Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions src/librustc/lint/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -938,10 +938,9 @@ pub struct NonSnakeCase;

impl NonSnakeCase {
fn check_snake_case(&self, cx: &Context, sort: &str, ident: ast::Ident, span: Span) {
fn is_snake_case(ident: ast::Ident) -> bool {
let ident = token::get_ident(ident);
if ident.get().is_empty() { return true; }
let ident = ident.get().trim_left_matches('\'');
fn is_snake_case(ident: &str) -> bool {
if ident.is_empty() { return true; }
let ident = ident.trim_left_matches('\'');
let ident = ident.trim_matches('_');

let mut allow_underscore = true;
Expand Down Expand Up @@ -978,10 +977,16 @@ impl NonSnakeCase {

let s = token::get_ident(ident);

if !is_snake_case(ident) {
cx.span_lint(NON_SNAKE_CASE, span,
&format!("{} `{}` should have a snake case name such as `{}`",
sort, s, to_snake_case(s.get()))[]);
if !is_snake_case(s.get()) {
let snaked = to_snake_case(s.get());
if is_snake_case(&*snaked) {
cx.span_lint(NON_SNAKE_CASE, span,
&format!("{} `{}` should have a snake case name such as `{}`",
sort, s, snaked)[]);
} else {
cx.span_lint(NON_SNAKE_CASE, span,
&format!("{} `{}` should have a snake case name", sort, s)[]);
}
}
}
}
Expand Down
10 changes: 10 additions & 0 deletions src/test/compile-fail/lint-non-snake-case-functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#![deny(non_snake_case)]
#![allow(dead_code)]
#![feature(non_ascii_idents)]

struct Foo;

Expand All @@ -26,6 +27,9 @@ impl Foo {

fn render_HTML() {}
//~^ ERROR method `render_HTML` should have a snake case name such as `render_html`

fn を() {}
//~^ ERROR method `を` should have a snake case name
}

trait X {
Expand All @@ -37,6 +41,9 @@ trait X {

fn something__else(&mut self);
//~^ ERROR trait method `something__else` should have a snake case name such as `something_else`

fn あ() {}
//~^ ERROR method `あ` should have a snake case name
}

impl X for Foo {
Expand All @@ -51,4 +58,7 @@ fn Cookie() {}
pub fn bi_S_Cuit() {}
//~^ ERROR function `bi_S_Cuit` should have a snake case name such as `bi_s_cuit`

pub fn お() {}
//~^ ERROR function `お` should have a snake case name

fn main() { }