From e477a13d63c2139f39c192c8e22dcfd0810f68e4 Mon Sep 17 00:00:00 2001 From: Corey Farwell Date: Wed, 29 Aug 2018 08:21:01 -0500 Subject: Replace usages of 'bad_style' with 'nonstandard_style'. `bad_style` is being deprecated in favor of `nonstandard_style`: - https://github.com/rust-lang/rust/issues/41646 --- src/bootstrap/job.rs | 2 +- src/bootstrap/util.rs | 2 +- src/liballoc_system/lib.rs | 2 +- src/libpanic_unwind/seh.rs | 2 +- src/libpanic_unwind/seh64_gnu.rs | 2 +- src/libpanic_unwind/windows.rs | 2 +- src/librustc/ty/query/plumbing.rs | 4 +- src/librustc/util/common.rs | 2 +- src/librustc/util/profiling.rs | 2 +- src/librustc_data_structures/flock.rs | 2 +- src/librustc_errors/lock.rs | 2 +- src/librustc_lint/bad_style.rs | 416 ------------------------- src/librustc_lint/builtin.rs | 2 +- src/librustc_lint/lib.rs | 4 +- src/librustc_lint/nonstandard_style.rs | 416 +++++++++++++++++++++++++ src/librustc_platform_intrinsics/lib.rs | 2 +- src/libstd/os/mod.rs | 2 +- src/libstd/sys/redox/mod.rs | 2 +- src/libstd/sys/unix/mod.rs | 2 +- src/libstd/sys/wasm/net.rs | 2 +- src/libstd/sys/windows/c.rs | 2 +- src/libstd/sys/windows/mod.rs | 2 +- src/libstd/sys/windows/os.rs | 2 +- src/libtest/lib.rs | 2 +- src/libunwind/libunwind.rs | 2 +- src/test/run-pass/simd-target-feature-mixup.rs | 2 +- src/test/rustdoc/intra-links.rs | 2 +- src/test/ui/lint/lint-group-style.rs | 8 +- src/test/ui/lint/lint-group-style.stderr | 30 +- src/test/ui/lint/lint-shorthand-field.rs | 2 +- src/test/ui/lint/outer-forbid.rs | 4 +- src/test/ui/lint/outer-forbid.stderr | 6 +- 32 files changed, 468 insertions(+), 468 deletions(-) delete mode 100644 src/librustc_lint/bad_style.rs create mode 100644 src/librustc_lint/nonstandard_style.rs (limited to 'src') diff --git a/src/bootstrap/job.rs b/src/bootstrap/job.rs index 6445ce8da33..e6ee525ca2e 100644 --- a/src/bootstrap/job.rs +++ b/src/bootstrap/job.rs @@ -37,7 +37,7 @@ //! Note that this module has a #[cfg(windows)] above it as none of this logic //! is required on Unix. -#![allow(bad_style, dead_code)] +#![allow(nonstandard_style, dead_code)] use std::env; use std::io; diff --git a/src/bootstrap/util.rs b/src/bootstrap/util.rs index be03796921a..8ce8f20add3 100644 --- a/src/bootstrap/util.rs +++ b/src/bootstrap/util.rs @@ -137,7 +137,7 @@ pub fn symlink_dir(config: &Config, src: &Path, dest: &Path) -> io::Result<()> { // // Copied from std #[cfg(windows)] - #[allow(bad_style)] + #[allow(nonstandard_style)] fn symlink_dir_inner(target: &Path, junction: &Path) -> io::Result<()> { use std::ptr; use std::ffi::OsStr; diff --git a/src/liballoc_system/lib.rs b/src/liballoc_system/lib.rs index ffab9e8e4af..8848be59038 100644 --- a/src/liballoc_system/lib.rs +++ b/src/liballoc_system/lib.rs @@ -220,7 +220,7 @@ mod platform { } #[cfg(windows)] -#[allow(bad_style)] +#[allow(nonstandard_style)] mod platform { use MIN_ALIGN; use System; diff --git a/src/libpanic_unwind/seh.rs b/src/libpanic_unwind/seh.rs index 015be2dea21..d6298a38a26 100644 --- a/src/libpanic_unwind/seh.rs +++ b/src/libpanic_unwind/seh.rs @@ -54,7 +54,7 @@ //! [win64]: http://msdn.microsoft.com/en-us/library/1eyas8tf.aspx //! [llvm]: http://llvm.org/docs/ExceptionHandling.html#background-on-windows-exceptions -#![allow(bad_style)] +#![allow(nonstandard_style)] #![allow(private_no_mangle_fns)] use alloc::boxed::Box; diff --git a/src/libpanic_unwind/seh64_gnu.rs b/src/libpanic_unwind/seh64_gnu.rs index 0b08e54c673..c2074db0038 100644 --- a/src/libpanic_unwind/seh64_gnu.rs +++ b/src/libpanic_unwind/seh64_gnu.rs @@ -11,7 +11,7 @@ //! Unwinding implementation of top of native Win64 SEH, //! however the unwind handler data (aka LSDA) uses GCC-compatible encoding. -#![allow(bad_style)] +#![allow(nonstandard_style)] #![allow(private_no_mangle_fns)] use alloc::boxed::Box; diff --git a/src/libpanic_unwind/windows.rs b/src/libpanic_unwind/windows.rs index 5f1dda36a88..0a1c9b3adf1 100644 --- a/src/libpanic_unwind/windows.rs +++ b/src/libpanic_unwind/windows.rs @@ -8,7 +8,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -#![allow(bad_style)] +#![allow(nonstandard_style)] #![allow(dead_code)] #![cfg(windows)] diff --git a/src/librustc/ty/query/plumbing.rs b/src/librustc/ty/query/plumbing.rs index 8473e4af40e..0edb1aa79e7 100644 --- a/src/librustc/ty/query/plumbing.rs +++ b/src/librustc/ty/query/plumbing.rs @@ -718,7 +718,7 @@ macro_rules! define_queries_inner { } } - #[allow(bad_style)] + #[allow(nonstandard_style)] #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)] pub enum Query<$tcx> { $($(#[$attr])* $name($K)),* @@ -775,7 +775,7 @@ macro_rules! define_queries_inner { pub mod queries { use std::marker::PhantomData; - $(#[allow(bad_style)] + $(#[allow(nonstandard_style)] pub struct $name<$tcx> { data: PhantomData<&$tcx ()> })* diff --git a/src/librustc/util/common.rs b/src/librustc/util/common.rs index bdfba7c3e3a..02bdc5f41b3 100644 --- a/src/librustc/util/common.rs +++ b/src/librustc/util/common.rs @@ -84,7 +84,7 @@ pub struct ProfQDumpParams { pub dump_profq_msg_log:bool, } -#[allow(bad_style)] +#[allow(nonstandard_style)] #[derive(Clone, Debug, PartialEq, Eq)] pub struct QueryMsg { pub query: &'static str, diff --git a/src/librustc/util/profiling.rs b/src/librustc/util/profiling.rs index 74ff1a5f4fd..70760d35f78 100644 --- a/src/librustc/util/profiling.rs +++ b/src/librustc/util/profiling.rs @@ -21,7 +21,7 @@ macro_rules! define_categories { $($name),* } - #[allow(bad_style)] + #[allow(nonstandard_style)] struct Categories { $($name: T),* } diff --git a/src/librustc_data_structures/flock.rs b/src/librustc_data_structures/flock.rs index 3f248dadb66..f10a9a68bed 100644 --- a/src/librustc_data_structures/flock.rs +++ b/src/librustc_data_structures/flock.rs @@ -239,7 +239,7 @@ mod imp { } #[cfg(windows)] -#[allow(bad_style)] +#[allow(nonstandard_style)] mod imp { use std::io; use std::mem; diff --git a/src/librustc_errors/lock.rs b/src/librustc_errors/lock.rs index dff8d53986d..e5baf93b000 100644 --- a/src/librustc_errors/lock.rs +++ b/src/librustc_errors/lock.rs @@ -22,7 +22,7 @@ use std::any::Any; #[cfg(windows)] -#[allow(bad_style)] +#[allow(nonstandard_style)] pub fn acquire_global_lock(name: &str) -> Box { use std::ffi::CString; use std::io; diff --git a/src/librustc_lint/bad_style.rs b/src/librustc_lint/bad_style.rs deleted file mode 100644 index 09871c0e840..00000000000 --- a/src/librustc_lint/bad_style.rs +++ /dev/null @@ -1,416 +0,0 @@ -// Copyright 2015 The Rust Project Developers. See the COPYRIGHT -// file at the top-level directory of this distribution and at -// http://rust-lang.org/COPYRIGHT. -// -// Licensed under the Apache License, Version 2.0 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -use rustc::hir::def::Def; -use rustc::ty; -use lint::{LateContext, LintContext, LintArray}; -use lint::{LintPass, LateLintPass}; - -use rustc_target::spec::abi::Abi; -use syntax::ast; -use syntax::attr; -use syntax_pos::Span; - -use rustc::hir::{self, GenericParamKind, PatKind}; -use rustc::hir::intravisit::FnKind; - -#[derive(PartialEq)] -pub enum MethodLateContext { - TraitAutoImpl, - TraitImpl, - PlainImpl, -} - -pub fn method_context(cx: &LateContext, id: ast::NodeId) -> MethodLateContext { - let def_id = cx.tcx.hir.local_def_id(id); - let item = cx.tcx.associated_item(def_id); - match item.container { - ty::TraitContainer(..) => MethodLateContext::TraitAutoImpl, - ty::ImplContainer(cid) => { - match cx.tcx.impl_trait_ref(cid) { - Some(_) => MethodLateContext::TraitImpl, - None => MethodLateContext::PlainImpl, - } - } - } -} - -declare_lint! { - pub NON_CAMEL_CASE_TYPES, - Warn, - "types, variants, traits and type parameters should have camel case names" -} - -#[derive(Copy, Clone)] -pub struct NonCamelCaseTypes; - -impl NonCamelCaseTypes { - fn check_case(&self, cx: &LateContext, sort: &str, name: ast::Name, span: Span) { - fn char_has_case(c: char) -> bool { - c.is_lowercase() || c.is_uppercase() - } - - fn is_camel_case(name: ast::Name) -> bool { - let name = name.as_str(); - if name.is_empty() { - return true; - } - let name = name.trim_matches('_'); - - // start with a non-lowercase letter rather than non-uppercase - // ones (some scripts don't have a concept of upper/lowercase) - !name.is_empty() && !name.chars().next().unwrap().is_lowercase() && - !name.contains("__") && !name.chars().collect::>().windows(2).any(|pair| { - // contains a capitalisable character followed by, or preceded by, an underscore - char_has_case(pair[0]) && pair[1] == '_' || - char_has_case(pair[1]) && pair[0] == '_' - }) - } - - fn to_camel_case(s: &str) -> String { - s.trim_matches('_') - .split('_') - .map(|word| { - word.chars().enumerate().map(|(i, c)| if i == 0 { - c.to_uppercase().collect::() - } else { - c.to_lowercase().collect() - }) - .collect::() - }) - .filter(|x| !x.is_empty()) - .fold((String::new(), None), |(acc, prev): (String, Option), next| { - // separate two components with an underscore if their boundary cannot - // be distinguished using a uppercase/lowercase case distinction - let join = if let Some(prev) = prev { - let l = prev.chars().last().unwrap(); - let f = next.chars().next().unwrap(); - !char_has_case(l) && !char_has_case(f) - } else { false }; - (acc + if join { "_" } else { "" } + &next, Some(next)) - }).0 - } - - if !is_camel_case(name) { - let c = to_camel_case(&name.as_str()); - let m = if c.is_empty() { - format!("{} `{}` should have a camel case name such as `CamelCase`", sort, name) - } else { - format!("{} `{}` should have a camel case name such as `{}`", sort, name, c) - }; - cx.span_lint(NON_CAMEL_CASE_TYPES, span, &m); - } - } -} - -impl LintPass for NonCamelCaseTypes { - fn get_lints(&self) -> LintArray { - lint_array!(NON_CAMEL_CASE_TYPES) - } -} - -impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NonCamelCaseTypes { - fn check_item(&mut self, cx: &LateContext, it: &hir::Item) { - let has_repr_c = it.attrs - .iter() - .any(|attr| { - attr::find_repr_attrs(cx.tcx.sess.diagnostic(), attr) - .iter() - .any(|r| r == &attr::ReprC) - }); - - if has_repr_c { - return; - } - - match it.node { - hir::ItemKind::Ty(..) | - hir::ItemKind::Enum(..) | - hir::ItemKind::Struct(..) | - hir::ItemKind::Union(..) => self.check_case(cx, "type", it.name, it.span), - hir::ItemKind::Trait(..) => self.check_case(cx, "trait", it.name, it.span), - _ => (), - } - } - - fn check_variant(&mut self, cx: &LateContext, v: &hir::Variant, _: &hir::Generics) { - self.check_case(cx, "variant", v.node.name, v.span); - } - - fn check_generic_param(&mut self, cx: &LateContext, param: &hir::GenericParam) { - match param.kind { - GenericParamKind::Lifetime { .. } => {} - GenericParamKind::Type { synthetic, .. } => { - if synthetic.is_none() { - self.check_case(cx, "type parameter", param.name.ident().name, param.span); - } - } - } - } -} - -declare_lint! { - pub NON_SNAKE_CASE, - Warn, - "variables, methods, functions, lifetime parameters and modules should have snake case names" -} - -#[derive(Copy, Clone)] -pub struct NonSnakeCase; - -impl NonSnakeCase { - fn to_snake_case(mut str: &str) -> String { - let mut words = vec![]; - // Preserve leading underscores - str = str.trim_left_matches(|c: char| { - if c == '_' { - words.push(String::new()); - true - } else { - false - } - }); - for s in str.split('_') { - let mut last_upper = false; - let mut buf = String::new(); - if s.is_empty() { - continue; - } - for ch in s.chars() { - if !buf.is_empty() && buf != "'" && ch.is_uppercase() && !last_upper { - words.push(buf); - buf = String::new(); - } - last_upper = ch.is_uppercase(); - buf.extend(ch.to_lowercase()); - } - words.push(buf); - } - words.join("_") - } - - fn check_snake_case(&self, cx: &LateContext, sort: &str, name: &str, span: Option) { - 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; - ident.chars().all(|c| { - allow_underscore = match c { - '_' if !allow_underscore => return false, - '_' => false, - // It would be more obvious to use `c.is_lowercase()`, - // but some characters do not have a lowercase form - c if !c.is_uppercase() => true, - _ => return false, - }; - true - }) - } - - if !is_snake_case(name) { - let sc = NonSnakeCase::to_snake_case(name); - let msg = if sc != name { - format!("{} `{}` should have a snake case name such as `{}`", - sort, - name, - sc) - } else { - format!("{} `{}` should have a snake case name", sort, name) - }; - match span { - Some(span) => cx.span_lint(NON_SNAKE_CASE, span, &msg), - None => cx.lint(NON_SNAKE_CASE, &msg), - } - } - } -} - -impl LintPass for NonSnakeCase { - fn get_lints(&self) -> LintArray { - lint_array!(NON_SNAKE_CASE) - } -} - -impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NonSnakeCase { - fn check_crate(&mut self, cx: &LateContext, cr: &hir::Crate) { - let attr_crate_name = attr::find_by_name(&cr.attrs, "crate_name") - .and_then(|at| at.value_str().map(|s| (at, s))); - if let Some(ref name) = cx.tcx.sess.opts.crate_name { - self.check_snake_case(cx, "crate", name, None); - } else if let Some((attr, name)) = attr_crate_name { - self.check_snake_case(cx, "crate", &name.as_str(), Some(attr.span)); - } - } - - fn check_generic_param(&mut self, cx: &LateContext, param: &hir::GenericParam) { - match param.kind { - GenericParamKind::Lifetime { .. } => { - let name = param.name.ident().as_str(); - self.check_snake_case(cx, "lifetime", &name, Some(param.span)); - } - GenericParamKind::Type { .. } => {} - } - } - - fn check_fn(&mut self, - cx: &LateContext, - fk: FnKind, - _: &hir::FnDecl, - _: &hir::Body, - span: Span, - id: ast::NodeId) { - match fk { - FnKind::Method(name, ..) => { - match method_context(cx, id) { - MethodLateContext::PlainImpl => { - self.check_snake_case(cx, "method", &name.as_str(), Some(span)) - } - MethodLateContext::TraitAutoImpl => { - self.check_snake_case(cx, "trait method", &name.as_str(), Some(span)) - } - _ => (), - } - } - FnKind::ItemFn(name, _, header, _, attrs) => { - // Skip foreign-ABI #[no_mangle] functions (Issue #31924) - if header.abi != Abi::Rust && attr::find_by_name(attrs, "no_mangle").is_some() { - return; - } - self.check_snake_case(cx, "function", &name.as_str(), Some(span)) - } - FnKind::Closure(_) => (), - } - } - - fn check_item(&mut self, cx: &LateContext, it: &hir::Item) { - if let hir::ItemKind::Mod(_) = it.node { - self.check_snake_case(cx, "module", &it.name.as_str(), Some(it.span)); - } - } - - fn check_trait_item(&mut self, cx: &LateContext, item: &hir::TraitItem) { - if let hir::TraitItemKind::Method(_, hir::TraitMethod::Required(ref pnames)) = item.node { - self.check_snake_case(cx, - "trait method", - &item.ident.as_str(), - Some(item.span)); - for param_name in pnames { - self.check_snake_case(cx, "variable", ¶m_name.as_str(), Some(param_name.span)); - } - } - } - - fn check_pat(&mut self, cx: &LateContext, p: &hir::Pat) { - if let &PatKind::Binding(_, _, ref ident, _) = &p.node { - self.check_snake_case(cx, "variable", &ident.as_str(), Some(p.span)); - } - } - - fn check_struct_def(&mut self, - cx: &LateContext, - s: &hir::VariantData, - _: ast::Name, - _: &hir::Generics, - _: ast::NodeId) { - for sf in s.fields() { - self.check_snake_case(cx, "structure field", &sf.ident.as_str(), Some(sf.span)); - } - } -} - -declare_lint! { - pub NON_UPPER_CASE_GLOBALS, - Warn, - "static constants should have uppercase identifiers" -} - -#[derive(Copy, Clone)] -pub struct NonUpperCaseGlobals; - -impl NonUpperCaseGlobals { - fn check_upper_case(cx: &LateContext, sort: &str, name: ast::Name, span: Span) { - if name.as_str().chars().any(|c| c.is_lowercase()) { - let uc = NonSnakeCase::to_snake_case(&name.as_str()).to_uppercase(); - if name != &*uc { - cx.span_lint(NON_UPPER_CASE_GLOBALS, - span, - &format!("{} `{}` should have an upper case name such as `{}`", - sort, - name, - uc)); - } else { - cx.span_lint(NON_UPPER_CASE_GLOBALS, - span, - &format!("{} `{}` should have an upper case name", sort, name)); - } - } - } -} - -impl LintPass for NonUpperCaseGlobals { - fn get_lints(&self) -> LintArray { - lint_array!(NON_UPPER_CASE_GLOBALS) - } -} - -impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NonUpperCaseGlobals { - fn check_item(&mut self, cx: &LateContext, it: &hir::Item) { - match it.node { - hir::ItemKind::Static(..) => { - if attr::find_by_name(&it.attrs, "no_mangle").is_some() { - return; - } - NonUpperCaseGlobals::check_upper_case(cx, "static variable", it.name, it.span); - } - hir::ItemKind::Const(..) => { - NonUpperCaseGlobals::check_upper_case(cx, "constant", it.name, it.span); - } - _ => {} - } - } - - fn check_trait_item(&mut self, cx: &LateContext, ti: &hir::TraitItem) { - match ti.node { - hir::TraitItemKind::Const(..) => { - NonUpperCaseGlobals::check_upper_case(cx, "associated constant", - ti.ident.name, ti.span); - } - _ => {} - } - } - - fn check_impl_item(&mut self, cx: &LateContext, ii: &hir::ImplItem) { - match ii.node { - hir::ImplItemKind::Const(..) => { - NonUpperCaseGlobals::check_upper_case(cx, "associated constant", - ii.ident.name, ii.span); - } - _ => {} - } - } - - fn check_pat(&mut self, cx: &LateContext, p: &hir::Pat) { - // Lint for constants that look like binding identifiers (#7526) - if let PatKind::Path(hir::QPath::Resolved(None, ref path)) = p.node { - if let Def::Const(..) = path.def { - if path.segments.len() == 1 { - NonUpperCaseGlobals::check_upper_case(cx, - "constant in pattern", - path.segments[0].ident.name, - path.span); - } - } - } - } -} diff --git a/src/librustc_lint/builtin.rs b/src/librustc_lint/builtin.rs index c497627415f..acce4bc0650 100644 --- a/src/librustc_lint/builtin.rs +++ b/src/librustc_lint/builtin.rs @@ -54,7 +54,7 @@ use syntax::errors::{Applicability, DiagnosticBuilder}; use rustc::hir::{self, GenericParamKind, PatKind}; use rustc::hir::intravisit::FnKind; -use bad_style::{MethodLateContext, method_context}; +use nonstandard_style::{MethodLateContext, method_context}; // hardwired lints from librustc pub use lint::builtin::*; diff --git a/src/librustc_lint/lib.rs b/src/librustc_lint/lib.rs index 05b11e3ba3a..0fcf0ca30c1 100644 --- a/src/librustc_lint/lib.rs +++ b/src/librustc_lint/lib.rs @@ -62,12 +62,12 @@ use syntax::edition::Edition; use lint::LintId; use lint::FutureIncompatibleInfo; -mod bad_style; +mod nonstandard_style; pub mod builtin; mod types; mod unused; -use bad_style::*; +use nonstandard_style::*; use builtin::*; use types::*; use unused::*; diff --git a/src/librustc_lint/nonstandard_style.rs b/src/librustc_lint/nonstandard_style.rs new file mode 100644 index 00000000000..09871c0e840 --- /dev/null +++ b/src/librustc_lint/nonstandard_style.rs @@ -0,0 +1,416 @@ +// Copyright 2015 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +use rustc::hir::def::Def; +use rustc::ty; +use lint::{LateContext, LintContext, LintArray}; +use lint::{LintPass, LateLintPass}; + +use rustc_target::spec::abi::Abi; +use syntax::ast; +use syntax::attr; +use syntax_pos::Span; + +use rustc::hir::{self, GenericParamKind, PatKind}; +use rustc::hir::intravisit::FnKind; + +#[derive(PartialEq)] +pub enum MethodLateContext { + TraitAutoImpl, + TraitImpl, + PlainImpl, +} + +pub fn method_context(cx: &LateContext, id: ast::NodeId) -> MethodLateContext { + let def_id = cx.tcx.hir.local_def_id(id); + let item = cx.tcx.associated_item(def_id); + match item.container { + ty::TraitContainer(..) => MethodLateContext::TraitAutoImpl, + ty::ImplContainer(cid) => { + match cx.tcx.impl_trait_ref(cid) { + Some(_) => MethodLateContext::TraitImpl, + None => MethodLateContext::PlainImpl, + } + } + } +} + +declare_lint! { + pub NON_CAMEL_CASE_TYPES, + Warn, + "types, variants, traits and type parameters should have camel case names" +} + +#[derive(Copy, Clone)] +pub struct NonCamelCaseTypes; + +impl NonCamelCaseTypes { + fn check_case(&self, cx: &LateContext, sort: &str, name: ast::Name, span: Span) { + fn char_has_case(c: char) -> bool { + c.is_lowercase() || c.is_uppercase() + } + + fn is_camel_case(name: ast::Name) -> bool { + let name = name.as_str(); + if name.is_empty() { + return true; + } + let name = name.trim_matches('_'); + + // start with a non-lowercase letter rather than non-uppercase + // ones (some scripts don't have a concept of upper/lowercase) + !name.is_empty() && !name.chars().next().unwrap().is_lowercase() && + !name.contains("__") && !name.chars().collect::>().windows(2).any(|pair| { + // contains a capitalisable character followed by, or preceded by, an underscore + char_has_case(pair[0]) && pair[1] == '_' || + char_has_case(pair[1]) && pair[0] == '_' + }) + } + + fn to_camel_case(s: &str) -> String { + s.trim_matches('_') + .split('_') + .map(|word| { + word.chars().enumerate().map(|(i, c)| if i == 0 { + c.to_uppercase().collect::() + } else { + c.to_lowercase().collect() + }) + .collect::() + }) + .filter(|x| !x.is_empty()) + .fold((String::new(), None), |(acc, prev): (String, Option), next| { + // separate two components with an underscore if their boundary cannot + // be distinguished using a uppercase/lowercase case distinction + let join = if let Some(prev) = prev { + let l = prev.chars().last().unwrap(); + let f = next.chars().next().unwrap(); + !char_has_case(l) && !char_has_case(f) + } else { false }; + (acc + if join { "_" } else { "" } + &next, Some(next)) + }).0 + } + + if !is_camel_case(name) { + let c = to_camel_case(&name.as_str()); + let m = if c.is_empty() { + format!("{} `{}` should have a camel case name such as `CamelCase`", sort, name) + } else { + format!("{} `{}` should have a camel case name such as `{}`", sort, name, c) + }; + cx.span_lint(NON_CAMEL_CASE_TYPES, span, &m); + } + } +} + +impl LintPass for NonCamelCaseTypes { + fn get_lints(&self) -> LintArray { + lint_array!(NON_CAMEL_CASE_TYPES) + } +} + +impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NonCamelCaseTypes { + fn check_item(&mut self, cx: &LateContext, it: &hir::Item) { + let has_repr_c = it.attrs + .iter() + .any(|attr| { + attr::find_repr_attrs(cx.tcx.sess.diagnostic(), attr) + .iter() + .any(|r| r == &attr::ReprC) + }); + + if has_repr_c { + return; + } + + match it.node { + hir::ItemKind::Ty(..) | + hir::ItemKind::Enum(..) | + hir::ItemKind::Struct(..) | + hir::ItemKind::Union(..) => self.check_case(cx, "type", it.name, it.span), + hir::ItemKind::Trait(..) => self.check_case(cx, "trait", it.name, it.span), + _ => (), + } + } + + fn check_variant(&mut self, cx: &LateContext, v: &hir::Variant, _: &hir::Generics) { + self.check_case(cx, "variant", v.node.name, v.span); + } + + fn check_generic_param(&mut self, cx: &LateContext, param: &hir::GenericParam) { + match param.kind { + GenericParamKind::Lifetime { .. } => {} + GenericParamKind::Type { synthetic, .. } => { + if synthetic.is_none() { + self.check_case(cx, "type parameter", param.name.ident().name, param.span); + } + } + } + } +} + +declare_lint! { + pub NON_SNAKE_CASE, + Warn, + "variables, methods, functions, lifetime parameters and modules should have snake case names" +} + +#[derive(Copy, Clone)] +pub struct NonSnakeCase; + +impl NonSnakeCase { + fn to_snake_case(mut str: &str) -> String { + let mut words = vec![]; + // Preserve leading underscores + str = str.trim_left_matches(|c: char| { + if c == '_' { + words.push(String::new()); + true + } else { + false + } + }); + for s in str.split('_') { + let mut last_upper = false; + let mut buf = String::new(); + if s.is_empty() { + continue; + } + for ch in s.chars() { + if !buf.is_empty() && buf != "'" && ch.is_uppercase() && !last_upper { + words.push(buf); + buf = String::new(); + } + last_upper = ch.is_uppercase(); + buf.extend(ch.to_lowercase()); + } + words.push(buf); + } + words.join("_") + } + + fn check_snake_case(&self, cx: &LateContext, sort: &str, name: &str, span: Option) { + 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; + ident.chars().all(|c| { + allow_underscore = match c { + '_' if !allow_underscore => return false, + '_' => false, + // It would be more obvious to use `c.is_lowercase()`, + // but some characters do not have a lowercase form + c if !c.is_uppercase() => true, + _ => return false, + }; + true + }) + } + + if !is_snake_case(name) { + let sc = NonSnakeCase::to_snake_case(name); + let msg = if sc != name { + format!("{} `{}` should have a snake case name such as `{}`", + sort, + name, + sc) + } else { + format!("{} `{}` should have a snake case name", sort, name) + }; + match span { + Some(span) => cx.span_lint(NON_SNAKE_CASE, span, &msg), + None => cx.lint(NON_SNAKE_CASE, &msg), + } + } + } +} + +impl LintPass for NonSnakeCase { + fn get_lints(&self) -> LintArray { + lint_array!(NON_SNAKE_CASE) + } +} + +impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NonSnakeCase { + fn check_crate(&mut self, cx: &LateContext, cr: &hir::Crate) { + let attr_crate_name = attr::find_by_name(&cr.attrs, "crate_name") + .and_then(|at| at.value_str().map(|s| (at, s))); + if let Some(ref name) = cx.tcx.sess.opts.crate_name { + self.check_snake_case(cx, "crate", name, None); + } else if let Some((attr, name)) = attr_crate_name { + self.check_snake_case(cx, "crate", &name.as_str(), Some(attr.span)); + } + } + + fn check_generic_param(&mut self, cx: &LateContext, param: &hir::GenericParam) { + match param.kind { + GenericParamKind::Lifetime { .. } => { + let name = param.name.ident().as_str(); + self.check_snake_case(cx, "lifetime", &name, Some(param.span)); + } + GenericParamKind::Type { .. } => {} + } + } + + fn check_fn(&mut self, + cx: &LateContext, + fk: FnKind, + _: &hir::FnDecl, + _: &hir::Body, + span: Span, + id: ast::NodeId) { + match fk { + FnKind::Method(name, ..) => { + match method_context(cx, id) { + MethodLateContext::PlainImpl => { + self.check_snake_case(cx, "method", &name.as_str(), Some(span)) + } + MethodLateContext::TraitAutoImpl => { + self.check_snake_case(cx, "trait method", &name.as_str(), Some(span)) + } + _ => (), + } + } + FnKind::ItemFn(name, _, header, _, attrs) => { + // Skip foreign-ABI #[no_mangle] functions (Issue #31924) + if header.abi != Abi::Rust && attr::find_by_name(attrs, "no_mangle").is_some() { + return; + } + self.check_snake_case(cx, "function", &name.as_str(), Some(span)) + } + FnKind::Closure(_) => (), + } + } + + fn check_item(&mut self, cx: &LateContext, it: &hir::Item) { + if let hir::ItemKind::Mod(_) = it.node { + self.check_snake_case(cx, "module", &it.name.as_str(), Some(it.span)); + } + } + + fn check_trait_item(&mut self, cx: &LateContext, item: &hir::TraitItem) { + if let hir::TraitItemKind::Method(_, hir::TraitMethod::Required(ref pnames)) = item.node { + self.check_snake_case(cx, + "trait method", + &item.ident.as_str(), + Some(item.span)); + for param_name in pnames { + self.check_snake_case(cx, "variable", ¶m_name.as_str(), Some(param_name.span)); + } + } + } + + fn check_pat(&mut self, cx: &LateContext, p: &hir::Pat) { + if let &PatKind::Binding(_, _, ref ident, _) = &p.node { + self.check_snake_case(cx, "variable", &ident.as_str(), Some(p.span)); + } + } + + fn check_struct_def(&mut self, + cx: &LateContext, + s: &hir::VariantData, + _: ast::Name, + _: &hir::Generics, + _: ast::NodeId) { + for sf in s.fields() { + self.check_snake_case(cx, "structure field", &sf.ident.as_str(), Some(sf.span)); + } + } +} + +declare_lint! { + pub NON_UPPER_CASE_GLOBALS, + Warn, + "static constants should have uppercase identifiers" +} + +#[derive(Copy, Clone)] +pub struct NonUpperCaseGlobals; + +impl NonUpperCaseGlobals { + fn check_upper_case(cx: &LateContext, sort: &str, name: ast::Name, span: Span) { + if name.as_str().chars().any(|c| c.is_lowercase()) { + let uc = NonSnakeCase::to_snake_case(&name.as_str()).to_uppercase(); + if name != &*uc { + cx.span_lint(NON_UPPER_CASE_GLOBALS, + span, + &format!("{} `{}` should have an upper case name such as `{}`", + sort, + name, + uc)); + } else { + cx.span_lint(NON_UPPER_CASE_GLOBALS, + span, + &format!("{} `{}` should have an upper case name", sort, name)); + } + } + } +} + +impl LintPass for NonUpperCaseGlobals { + fn get_lints(&self) -> LintArray { + lint_array!(NON_UPPER_CASE_GLOBALS) + } +} + +impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NonUpperCaseGlobals { + fn check_item(&mut self, cx: &LateContext, it: &hir::Item) { + match it.node { + hir::ItemKind::Static(..) => { + if attr::find_by_name(&it.attrs, "no_mangle").is_some() { + return; + } + NonUpperCaseGlobals::check_upper_case(cx, "static variable", it.name, it.span); + } + hir::ItemKind::Const(..) => { + NonUpperCaseGlobals::check_upper_case(cx, "constant", it.name, it.span); + } + _ => {} + } + } + + fn check_trait_item(&mut self, cx: &LateContext, ti: &hir::TraitItem) { + match ti.node { + hir::TraitItemKind::Const(..) => { + NonUpperCaseGlobals::check_upper_case(cx, "associated constant", + ti.ident.name, ti.span); + } + _ => {} + } + } + + fn check_impl_item(&mut self, cx: &LateContext, ii: &hir::ImplItem) { + match ii.node { + hir::ImplItemKind::Const(..) => { + NonUpperCaseGlobals::check_upper_case(cx, "associated constant", + ii.ident.name, ii.span); + } + _ => {} + } + } + + fn check_pat(&mut self, cx: &LateContext, p: &hir::Pat) { + // Lint for constants that look like binding identifiers (#7526) + if let PatKind::Path(hir::QPath::Resolved(None, ref path)) = p.node { + if let Def::Const(..) = path.def { + if path.segments.len() == 1 { + NonUpperCaseGlobals::check_upper_case(cx, + "constant in pattern", + path.segments[0].ident.name, + path.span); + } + } + } + } +} diff --git a/src/librustc_platform_intrinsics/lib.rs b/src/librustc_platform_intrinsics/lib.rs index 62405150cd2..f093d672498 100644 --- a/src/librustc_platform_intrinsics/lib.rs +++ b/src/librustc_platform_intrinsics/lib.rs @@ -8,7 +8,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -#![allow(bad_style)] +#![allow(nonstandard_style)] #![cfg_attr(not(stage0), feature(nll))] #![cfg_attr(not(stage0), feature(infer_outlives_requirements))] diff --git a/src/libstd/os/mod.rs b/src/libstd/os/mod.rs index c384ec9168a..6d8298f01cd 100644 --- a/src/libstd/os/mod.rs +++ b/src/libstd/os/mod.rs @@ -11,7 +11,7 @@ //! OS-specific functionality. #![stable(feature = "os", since = "1.0.0")] -#![allow(missing_docs, bad_style, missing_debug_implementations)] +#![allow(missing_docs, nonstandard_style, missing_debug_implementations)] cfg_if! { if #[cfg(dox)] { diff --git a/src/libstd/sys/redox/mod.rs b/src/libstd/sys/redox/mod.rs index 4352b72c307..f943257c687 100644 --- a/src/libstd/sys/redox/mod.rs +++ b/src/libstd/sys/redox/mod.rs @@ -8,7 +8,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -#![allow(dead_code, missing_docs, bad_style)] +#![allow(dead_code, missing_docs, nonstandard_style)] use io::{self, ErrorKind}; diff --git a/src/libstd/sys/unix/mod.rs b/src/libstd/sys/unix/mod.rs index c738003caf1..2b9fbc9ef39 100644 --- a/src/libstd/sys/unix/mod.rs +++ b/src/libstd/sys/unix/mod.rs @@ -8,7 +8,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -#![allow(missing_docs, bad_style)] +#![allow(missing_docs, nonstandard_style)] use io::{self, ErrorKind}; use libc; diff --git a/src/libstd/sys/wasm/net.rs b/src/libstd/sys/wasm/net.rs index e7476ab37f7..03a5b2d779e 100644 --- a/src/libstd/sys/wasm/net.rs +++ b/src/libstd/sys/wasm/net.rs @@ -297,7 +297,7 @@ pub fn lookup_host(_: &str) -> io::Result { unsupported() } -#[allow(bad_style)] +#[allow(nonstandard_style)] pub mod netc { pub const AF_INET: u8 = 0; pub const AF_INET6: u8 = 1; diff --git a/src/libstd/sys/windows/c.rs b/src/libstd/sys/windows/c.rs index e514a56dcc4..8a744519e91 100644 --- a/src/libstd/sys/windows/c.rs +++ b/src/libstd/sys/windows/c.rs @@ -10,7 +10,7 @@ //! C definitions used by libnative that don't belong in liblibc -#![allow(bad_style)] +#![allow(nonstandard_style)] #![cfg_attr(test, allow(dead_code))] #![unstable(issue = "0", feature = "windows_c")] diff --git a/src/libstd/sys/windows/mod.rs b/src/libstd/sys/windows/mod.rs index ccf79de909f..31ef9fa2bed 100644 --- a/src/libstd/sys/windows/mod.rs +++ b/src/libstd/sys/windows/mod.rs @@ -8,7 +8,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -#![allow(missing_docs, bad_style)] +#![allow(missing_docs, nonstandard_style)] use ptr; use ffi::{OsStr, OsString}; diff --git a/src/libstd/sys/windows/os.rs b/src/libstd/sys/windows/os.rs index b9448243559..29ea82c2053 100644 --- a/src/libstd/sys/windows/os.rs +++ b/src/libstd/sys/windows/os.rs @@ -10,7 +10,7 @@ //! Implementation of `std::os` functionality for Windows -#![allow(bad_style)] +#![allow(nonstandard_style)] use os::windows::prelude::*; diff --git a/src/libtest/lib.rs b/src/libtest/lib.rs index 29d7cfd2a3a..d993c6244fc 100644 --- a/src/libtest/lib.rs +++ b/src/libtest/lib.rs @@ -1184,7 +1184,7 @@ fn get_concurrency() -> usize { }; #[cfg(windows)] - #[allow(bad_style)] + #[allow(nonstandard_style)] fn num_cpus() -> usize { #[repr(C)] struct SYSTEM_INFO { diff --git a/src/libunwind/libunwind.rs b/src/libunwind/libunwind.rs index 73a259bd443..43c3e1e7666 100644 --- a/src/libunwind/libunwind.rs +++ b/src/libunwind/libunwind.rs @@ -8,7 +8,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -#![allow(bad_style)] +#![allow(nonstandard_style)] macro_rules! cfg_if { ( $( if #[cfg( $meta:meta )] { $($it1:item)* } else { $($it2:item)* } )* ) => diff --git a/src/test/run-pass/simd-target-feature-mixup.rs b/src/test/run-pass/simd-target-feature-mixup.rs index 139da046452..a7fd9f299c0 100644 --- a/src/test/run-pass/simd-target-feature-mixup.rs +++ b/src/test/run-pass/simd-target-feature-mixup.rs @@ -52,7 +52,7 @@ fn is_sigill(status: ExitStatus) -> bool { } #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] -#[allow(bad_style)] +#[allow(nonstandard_style)] mod test { // An SSE type #[repr(simd)] diff --git a/src/test/rustdoc/intra-links.rs b/src/test/rustdoc/intra-links.rs index c822d0f8b21..81b81a9a141 100644 --- a/src/test/rustdoc/intra-links.rs +++ b/src/test/rustdoc/intra-links.rs @@ -75,7 +75,7 @@ pub static THIS_STATIC: usize = 5usize; pub trait SoAmbiguous {} -#[allow(bad_style)] +#[allow(nonstandard_style)] pub fn SoAmbiguous() {} diff --git a/src/test/ui/lint/lint-group-style.rs b/src/test/ui/lint/lint-group-style.rs index 9f33f57f48a..55d6168e6e0 100644 --- a/src/test/ui/lint/lint-group-style.rs +++ b/src/test/ui/lint/lint-group-style.rs @@ -8,16 +8,16 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -#![deny(bad_style)] +#![deny(nonstandard_style)] #![allow(dead_code)] fn CamelCase() {} //~ ERROR should have a snake -#[allow(bad_style)] +#[allow(nonstandard_style)] mod test { fn CamelCase() {} - #[forbid(bad_style)] + #[forbid(nonstandard_style)] mod bad { fn CamelCase() {} //~ ERROR should have a snake @@ -25,7 +25,7 @@ mod test { } mod warn { - #![warn(bad_style)] + #![warn(nonstandard_style)] fn CamelCase() {} //~ WARN should have a snake diff --git a/src/test/ui/lint/lint-group-style.stderr b/src/test/ui/lint/lint-group-style.stderr index c1b15160bc5..6b91ce5b93c 100644 --- a/src/test/ui/lint/lint-group-style.stderr +++ b/src/test/ui/lint/lint-group-style.stderr @@ -7,9 +7,9 @@ LL | fn CamelCase() {} //~ ERROR should have a snake note: lint level defined here --> $DIR/lint-group-style.rs:11:9 | -LL | #![deny(bad_style)] - | ^^^^^^^^^ - = note: #[deny(non_snake_case)] implied by #[deny(bad_style)] +LL | #![deny(nonstandard_style)] + | ^^^^^^^^^^^^^^^^^ + = note: #[deny(non_snake_case)] implied by #[deny(nonstandard_style)] error: function `CamelCase` should have a snake case name such as `camel_case` --> $DIR/lint-group-style.rs:22:9 @@ -20,9 +20,9 @@ LL | fn CamelCase() {} //~ ERROR should have a snake note: lint level defined here --> $DIR/lint-group-style.rs:20:14 | -LL | #[forbid(bad_style)] - | ^^^^^^^^^ - = note: #[forbid(non_snake_case)] implied by #[forbid(bad_style)] +LL | #[forbid(nonstandard_style)] + | ^^^^^^^^^^^^^^^^^ + = note: #[forbid(non_snake_case)] implied by #[forbid(nonstandard_style)] error: static variable `bad` should have an upper case name such as `BAD` --> $DIR/lint-group-style.rs:24:9 @@ -33,9 +33,9 @@ LL | static bad: isize = 1; //~ ERROR should have an upper note: lint level defined here --> $DIR/lint-group-style.rs:20:14 | -LL | #[forbid(bad_style)] - | ^^^^^^^^^ - = note: #[forbid(non_upper_case_globals)] implied by #[forbid(bad_style)] +LL | #[forbid(nonstandard_style)] + | ^^^^^^^^^^^^^^^^^ + = note: #[forbid(non_upper_case_globals)] implied by #[forbid(nonstandard_style)] warning: function `CamelCase` should have a snake case name such as `camel_case` --> $DIR/lint-group-style.rs:30:9 @@ -46,9 +46,9 @@ LL | fn CamelCase() {} //~ WARN should have a snake note: lint level defined here --> $DIR/lint-group-style.rs:28:17 | -LL | #![warn(bad_style)] - | ^^^^^^^^^ - = note: #[warn(non_snake_case)] implied by #[warn(bad_style)] +LL | #![warn(nonstandard_style)] + | ^^^^^^^^^^^^^^^^^ + = note: #[warn(non_snake_case)] implied by #[warn(nonstandard_style)] warning: type `snake_case` should have a camel case name such as `SnakeCase` --> $DIR/lint-group-style.rs:32:9 @@ -59,9 +59,9 @@ LL | struct snake_case; //~ WARN should have a camel note: lint level defined here --> $DIR/lint-group-style.rs:28:17 | -LL | #![warn(bad_style)] - | ^^^^^^^^^ - = note: #[warn(non_camel_case_types)] implied by #[warn(bad_style)] +LL | #![warn(nonstandard_style)] + | ^^^^^^^^^^^^^^^^^ + = note: #[warn(non_camel_case_types)] implied by #[warn(nonstandard_style)] error: aborting due to 3 previous errors diff --git a/src/test/ui/lint/lint-shorthand-field.rs b/src/test/ui/lint/lint-shorthand-field.rs index 97a976a493f..1e37ac0dc58 100644 --- a/src/test/ui/lint/lint-shorthand-field.rs +++ b/src/test/ui/lint/lint-shorthand-field.rs @@ -8,7 +8,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -#![allow(bad_style, unused_variables)] +#![allow(nonstandard_style, unused_variables)] #![deny(non_shorthand_field_patterns)] struct Foo { diff --git a/src/test/ui/lint/outer-forbid.rs b/src/test/ui/lint/outer-forbid.rs index d72f307b461..23e98cc22fc 100644 --- a/src/test/ui/lint/outer-forbid.rs +++ b/src/test/ui/lint/outer-forbid.rs @@ -11,7 +11,7 @@ // Forbidding a group (here, `unused`) overrules subsequent allowance of both // the group, and an individual lint in the group (here, `unused_variables`); // and, forbidding an individual lint (here, `non_snake_case`) overrules -// subsequent allowance of a lint group containing it (here, `bad_style`). See +// subsequent allowance of a lint group containing it (here, `nonstandard_style`). See // Issue #42873. #![forbid(unused, non_snake_case)] @@ -22,7 +22,7 @@ fn foo() {} #[allow(unused)] //~ ERROR overruled fn bar() {} -#[allow(bad_style)] //~ ERROR overruled +#[allow(nonstandard_style)] //~ ERROR overruled fn main() { println!("hello forbidden world") } diff --git a/src/test/ui/lint/outer-forbid.stderr b/src/test/ui/lint/outer-forbid.stderr index e49dcd4a2d1..c011b49eaee 100644 --- a/src/test/ui/lint/outer-forbid.stderr +++ b/src/test/ui/lint/outer-forbid.stderr @@ -16,14 +16,14 @@ LL | #![forbid(unused, non_snake_case)] LL | #[allow(unused)] //~ ERROR overruled | ^^^^^^ overruled by previous forbid -error[E0453]: allow(bad_style) overruled by outer forbid(non_snake_case) +error[E0453]: allow(nonstandard_style) overruled by outer forbid(non_snake_case) --> $DIR/outer-forbid.rs:25:9 | LL | #![forbid(unused, non_snake_case)] | -------------- `forbid` level set here ... -LL | #[allow(bad_style)] //~ ERROR overruled - | ^^^^^^^^^ overruled by previous forbid +LL | #[allow(nonstandard_style)] //~ ERROR overruled + | ^^^^^^^^^^^^^^^^^ overruled by previous forbid error: aborting due to 3 previous errors -- cgit 1.4.1-3-g733a5