From 8582fde15085794c054f1ceee4001e2e1646bcb5 Mon Sep 17 00:00:00 2001 From: Michael Sullivan Date: Thu, 25 Jul 2013 12:26:38 -0700 Subject: [PATCH] Improve the camel case warning a bit. --- src/librustc/middle/lint.rs | 19 +++++++++++-------- .../compile-fail/lint-non-camel-case-types.rs | 12 ++++++------ 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/src/librustc/middle/lint.rs b/src/librustc/middle/lint.rs index d6e6db8354a..aaf0460a4e1 100644 --- a/src/librustc/middle/lint.rs +++ b/src/librustc/middle/lint.rs @@ -827,23 +827,26 @@ fn is_camel_case(cx: ty::ctxt, ident: ast::ident) -> bool { !ident.contains_char('_') } - fn check_case(cx: &Context, ident: ast::ident, span: span) { + fn check_case(cx: &Context, sort: &str, ident: ast::ident, span: span) { if !is_camel_case(cx.tcx, ident) { - cx.span_lint(non_camel_case_types, span, - "type, variant, or trait should have \ - a camel case identifier"); + cx.span_lint( + non_camel_case_types, span, + fmt!("%s `%s` should have a camel case identifier", + sort, cx.tcx.sess.str_of(ident))); } } match it.node { - ast::item_ty(*) | ast::item_struct(*) | + ast::item_ty(*) | ast::item_struct(*) => { + check_case(cx, "type", it.ident, it.span) + } ast::item_trait(*) => { - check_case(cx, it.ident, it.span) + check_case(cx, "trait", it.ident, it.span) } ast::item_enum(ref enum_definition, _) => { - check_case(cx, it.ident, it.span); + check_case(cx, "type", it.ident, it.span); for enum_definition.variants.iter().advance |variant| { - check_case(cx, variant.node.name, variant.span); + check_case(cx, "variant", variant.node.name, variant.span); } } _ => () diff --git a/src/test/compile-fail/lint-non-camel-case-types.rs b/src/test/compile-fail/lint-non-camel-case-types.rs index 27c9ca64a93..2cabdfe5bb0 100644 --- a/src/test/compile-fail/lint-non-camel-case-types.rs +++ b/src/test/compile-fail/lint-non-camel-case-types.rs @@ -10,25 +10,25 @@ #[forbid(non_camel_case_types)]; -struct foo { //~ ERROR type, variant, or trait should have a camel case identifier +struct foo { //~ ERROR type `foo` should have a camel case identifier bar: int, } -enum foo2 { //~ ERROR type, variant, or trait should have a camel case identifier +enum foo2 { //~ ERROR type `foo2` should have a camel case identifier Bar } -struct foo3 { //~ ERROR type, variant, or trait should have a camel case identifier +struct foo3 { //~ ERROR type `foo3` should have a camel case identifier bar: int } -type foo4 = int; //~ ERROR type, variant, or trait should have a camel case identifier +type foo4 = int; //~ ERROR type `foo4` should have a camel case identifier enum Foo5 { - bar //~ ERROR type, variant, or trait should have a camel case identifier + bar //~ ERROR variant `bar` should have a camel case identifier } -trait foo6 { //~ ERROR type, variant, or trait should have a camel case identifier +trait foo6 { //~ ERROR trait `foo6` should have a camel case identifier } fn main() { } -- GitLab