2016-08-23 17:39:36 +00:00
|
|
|
//! Read configurations files.
|
|
|
|
|
2018-08-01 20:48:41 +00:00
|
|
|
#![deny(clippy::missing_docs_in_private_items)]
|
2016-08-23 17:39:36 +00:00
|
|
|
|
2020-03-01 03:23:33 +00:00
|
|
|
use rustc_ast::ast::{LitKind, MetaItemKind, NestedMetaItem};
|
2020-01-04 10:00:00 +00:00
|
|
|
use rustc_span::source_map;
|
2020-02-05 02:06:34 +00:00
|
|
|
use source_map::Span;
|
2020-10-06 06:26:16 +00:00
|
|
|
use std::lazy::SyncLazy;
|
2020-02-05 02:06:34 +00:00
|
|
|
use std::path::{Path, PathBuf};
|
2017-05-09 13:23:38 +00:00
|
|
|
use std::sync::Mutex;
|
2020-02-05 02:06:34 +00:00
|
|
|
use std::{env, fmt, fs, io};
|
2018-07-19 07:11:15 +00:00
|
|
|
|
2019-01-31 01:15:29 +00:00
|
|
|
/// Gets the configuration file from arguments.
|
2020-02-05 02:06:34 +00:00
|
|
|
pub fn file_from_args(args: &[NestedMetaItem]) -> Result<Option<PathBuf>, (&'static str, Span)> {
|
|
|
|
for arg in args.iter().filter_map(NestedMetaItem::meta_item) {
|
2020-08-02 10:17:20 +00:00
|
|
|
if arg.has_name(sym!(conf_file)) {
|
2019-09-27 15:16:06 +00:00
|
|
|
return match arg.kind {
|
2020-02-05 02:06:34 +00:00
|
|
|
MetaItemKind::Word | MetaItemKind::List(_) => Err(("`conf_file` must be a named value", arg.span)),
|
|
|
|
MetaItemKind::NameValue(ref value) => {
|
|
|
|
if let LitKind::Str(ref file, _) = value.kind {
|
2018-11-27 20:14:15 +00:00
|
|
|
Ok(Some(file.to_string().into()))
|
|
|
|
} else {
|
|
|
|
Err(("`conf_file` value must be a string", value.span))
|
|
|
|
}
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2016-11-23 20:19:03 +00:00
|
|
|
};
|
2016-02-21 19:11:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(None)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Error from reading a configuration file.
|
|
|
|
#[derive(Debug)]
|
2016-06-10 14:17:20 +00:00
|
|
|
pub enum Error {
|
2016-08-23 17:39:36 +00:00
|
|
|
/// An I/O error.
|
2016-06-10 14:23:17 +00:00
|
|
|
Io(io::Error),
|
2020-02-05 02:06:34 +00:00
|
|
|
/// Not valid toml or doesn't fit the expected config format
|
2017-05-09 13:23:38 +00:00
|
|
|
Toml(String),
|
2016-02-21 19:11:32 +00:00
|
|
|
}
|
|
|
|
|
2016-06-10 14:17:20 +00:00
|
|
|
impl fmt::Display for Error {
|
2020-02-05 02:06:34 +00:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
match self {
|
|
|
|
Self::Io(err) => err.fmt(f),
|
|
|
|
Self::Toml(err) => err.fmt(f),
|
2016-02-21 19:11:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-10 14:17:20 +00:00
|
|
|
impl From<io::Error> for Error {
|
2016-02-21 19:11:32 +00:00
|
|
|
fn from(e: io::Error) -> Self {
|
2019-07-31 00:25:35 +00:00
|
|
|
Self::Io(e)
|
2016-02-21 19:11:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-06 06:26:16 +00:00
|
|
|
/// Vec of errors that might be collected during config toml parsing
|
|
|
|
static ERRORS: SyncLazy<Mutex<Vec<Error>>> = SyncLazy::new(|| Mutex::new(Vec::new()));
|
2016-02-21 19:11:32 +00:00
|
|
|
|
2017-05-09 13:23:38 +00:00
|
|
|
macro_rules! define_Conf {
|
2020-02-05 02:06:34 +00:00
|
|
|
($(#[$doc:meta] ($config:ident, $config_str:literal: $Ty:ty, $default:expr),)+) => {
|
2017-05-09 13:23:38 +00:00
|
|
|
mod helpers {
|
2019-05-09 17:05:34 +00:00
|
|
|
use serde::Deserialize;
|
2017-05-09 13:23:38 +00:00
|
|
|
/// Type used to store lint configuration.
|
2018-09-03 21:12:50 +00:00
|
|
|
#[derive(Deserialize)]
|
2020-02-05 02:06:34 +00:00
|
|
|
#[serde(rename_all = "kebab-case", deny_unknown_fields)]
|
2017-05-09 13:23:38 +00:00
|
|
|
pub struct Conf {
|
2020-02-05 02:06:34 +00:00
|
|
|
$(
|
|
|
|
#[$doc]
|
|
|
|
#[serde(default = $config_str)]
|
|
|
|
#[serde(with = $config_str)]
|
|
|
|
pub $config: $Ty,
|
|
|
|
)+
|
2017-05-09 13:23:38 +00:00
|
|
|
#[allow(dead_code)]
|
|
|
|
#[serde(default)]
|
|
|
|
third_party: Option<::toml::Value>,
|
2016-02-21 19:11:32 +00:00
|
|
|
}
|
2020-02-05 02:06:34 +00:00
|
|
|
|
2017-05-09 13:23:38 +00:00
|
|
|
$(
|
2020-02-05 02:06:34 +00:00
|
|
|
mod $config {
|
2017-05-09 13:23:38 +00:00
|
|
|
use serde::Deserialize;
|
2020-03-23 16:06:15 +00:00
|
|
|
pub fn deserialize<'de, D: serde::Deserializer<'de>>(deserializer: D) -> Result<$Ty, D::Error> {
|
2020-02-05 02:06:34 +00:00
|
|
|
use super::super::{ERRORS, Error};
|
2020-10-06 06:26:16 +00:00
|
|
|
|
2020-02-05 02:06:34 +00:00
|
|
|
Ok(
|
|
|
|
<$Ty>::deserialize(deserializer).unwrap_or_else(|e| {
|
|
|
|
ERRORS
|
|
|
|
.lock()
|
|
|
|
.expect("no threading here")
|
|
|
|
.push(Error::Toml(e.to_string()));
|
|
|
|
super::$config()
|
|
|
|
})
|
|
|
|
)
|
2016-02-21 19:11:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-18 06:37:41 +00:00
|
|
|
#[must_use]
|
2020-02-05 02:06:34 +00:00
|
|
|
fn $config() -> $Ty {
|
|
|
|
let x = $default;
|
|
|
|
x
|
2017-05-09 13:23:38 +00:00
|
|
|
}
|
|
|
|
)+
|
2016-02-21 19:11:32 +00:00
|
|
|
}
|
|
|
|
};
|
2016-02-22 13:25:51 +00:00
|
|
|
}
|
2016-02-21 19:11:32 +00:00
|
|
|
|
2020-02-05 02:06:34 +00:00
|
|
|
pub use self::helpers::Conf;
|
2016-02-21 19:11:32 +00:00
|
|
|
define_Conf! {
|
2021-03-26 14:39:23 +00:00
|
|
|
/// Lint: REDUNDANT_FIELD_NAMES, REDUNDANT_STATIC_LIFETIMES, FILTER_MAP_NEXT, CHECKED_CONVERSIONS, MANUAL_RANGE_CONTAINS, USE_SELF, MEM_REPLACE_WITH_DEFAULT, MANUAL_NON_EXHAUSTIVE, OPTION_AS_REF_DEREF, MAP_UNWRAP_OR, MATCH_LIKE_MATCHES_MACRO, MANUAL_STRIP, MISSING_CONST_FOR_FN, UNNESTED_OR_PATTERNS, FROM_OVER_INTO, NEEDLESS_QUESTION_MARK, PTR_AS_PTR. The minimum rust version that the project supports
|
2020-10-21 12:47:34 +00:00
|
|
|
(msrv, "msrv": Option<String>, None),
|
2020-06-23 15:05:22 +00:00
|
|
|
/// Lint: BLACKLISTED_NAME. The list of blacklisted names to lint about. NB: `bar` is not here since it has legitimate uses
|
|
|
|
(blacklisted_names, "blacklisted_names": Vec<String>, ["foo", "baz", "quux"].iter().map(ToString::to_string).collect()),
|
2019-02-23 01:19:50 +00:00
|
|
|
/// Lint: COGNITIVE_COMPLEXITY. The maximum cognitive complexity a function can have
|
2020-02-05 02:06:34 +00:00
|
|
|
(cognitive_complexity_threshold, "cognitive_complexity_threshold": u64, 25),
|
2019-02-23 01:19:50 +00:00
|
|
|
/// DEPRECATED LINT: CYCLOMATIC_COMPLEXITY. Use the Cognitive Complexity lint instead.
|
2020-02-05 02:06:34 +00:00
|
|
|
(cyclomatic_complexity_threshold, "cyclomatic_complexity_threshold": Option<u64>, None),
|
2016-04-04 18:18:17 +00:00
|
|
|
/// Lint: DOC_MARKDOWN. The list of words this lint should not consider as identifiers needing ticks
|
2020-02-05 02:06:34 +00:00
|
|
|
(doc_valid_idents, "doc_valid_idents": Vec<String>, [
|
2017-05-06 19:31:54 +00:00
|
|
|
"KiB", "MiB", "GiB", "TiB", "PiB", "EiB",
|
2016-12-21 11:30:41 +00:00
|
|
|
"DirectX",
|
2017-03-03 11:28:15 +00:00
|
|
|
"ECMAScript",
|
2016-12-21 11:30:41 +00:00
|
|
|
"GPLv2", "GPLv3",
|
2018-01-02 12:51:35 +00:00
|
|
|
"GitHub", "GitLab",
|
2016-12-21 11:30:41 +00:00
|
|
|
"IPv4", "IPv6",
|
2020-05-28 13:45:24 +00:00
|
|
|
"ClojureScript", "CoffeeScript", "JavaScript", "PureScript", "TypeScript",
|
2018-01-02 12:51:35 +00:00
|
|
|
"NaN", "NaNs",
|
2020-09-01 13:05:19 +00:00
|
|
|
"OAuth", "GraphQL",
|
2020-05-28 13:45:24 +00:00
|
|
|
"OCaml",
|
2021-02-24 04:04:00 +00:00
|
|
|
"OpenGL", "OpenMP", "OpenSSH", "OpenSSL", "OpenStreetMap", "OpenDNS",
|
2020-11-27 00:42:37 +00:00
|
|
|
"WebGL",
|
2020-05-28 13:45:24 +00:00
|
|
|
"TensorFlow",
|
2016-12-21 11:30:41 +00:00
|
|
|
"TrueType",
|
|
|
|
"iOS", "macOS",
|
2017-09-18 20:40:00 +00:00
|
|
|
"TeX", "LaTeX", "BibTeX", "BibLaTeX",
|
2017-04-25 09:38:37 +00:00
|
|
|
"MinGW",
|
2018-12-11 18:37:43 +00:00
|
|
|
"CamelCase",
|
2020-02-05 02:06:34 +00:00
|
|
|
].iter().map(ToString::to_string).collect()),
|
2016-02-22 13:25:51 +00:00
|
|
|
/// Lint: TOO_MANY_ARGUMENTS. The maximum number of argument a function or method can have
|
2020-02-05 02:06:34 +00:00
|
|
|
(too_many_arguments_threshold, "too_many_arguments_threshold": u64, 7),
|
2016-02-22 13:25:51 +00:00
|
|
|
/// Lint: TYPE_COMPLEXITY. The maximum complexity a type can have
|
2020-02-05 02:06:34 +00:00
|
|
|
(type_complexity_threshold, "type_complexity_threshold": u64, 250),
|
2016-03-14 13:56:44 +00:00
|
|
|
/// Lint: MANY_SINGLE_CHAR_NAMES. The maximum number of single char bindings a scope may have
|
2020-04-15 15:58:26 +00:00
|
|
|
(single_char_binding_names_threshold, "single_char_binding_names_threshold": u64, 4),
|
2020-08-14 12:13:35 +00:00
|
|
|
/// Lint: BOXED_LOCAL, USELESS_VEC. The maximum size of objects (in bytes) that will be linted. Larger objects are ok on the heap
|
2020-02-05 02:06:34 +00:00
|
|
|
(too_large_for_stack, "too_large_for_stack": u64, 200),
|
2016-08-06 18:59:27 +00:00
|
|
|
/// Lint: ENUM_VARIANT_NAMES. The minimum number of enum variants for the lints about variant names to trigger
|
2020-02-05 02:06:34 +00:00
|
|
|
(enum_variant_name_threshold, "enum_variant_name_threshold": u64, 3),
|
2018-12-07 11:24:59 +00:00
|
|
|
/// Lint: LARGE_ENUM_VARIANT. The maximum size of a enum's variant to avoid box suggestion
|
2020-02-05 02:06:34 +00:00
|
|
|
(enum_variant_size_threshold, "enum_variant_size_threshold": u64, 200),
|
2017-09-26 15:54:08 +00:00
|
|
|
/// Lint: VERBOSE_BIT_MASK. The maximum allowed size of a bit mask before suggesting to use 'trailing_zeros'
|
2020-02-05 02:06:34 +00:00
|
|
|
(verbose_bit_mask_threshold, "verbose_bit_mask_threshold": u64, 1),
|
2018-01-23 14:29:31 +00:00
|
|
|
/// Lint: DECIMAL_LITERAL_REPRESENTATION. The lower bound for linting decimal literals
|
2020-02-05 02:06:34 +00:00
|
|
|
(literal_representation_threshold, "literal_representation_threshold": u64, 16384),
|
2018-05-27 14:04:45 +00:00
|
|
|
/// Lint: TRIVIALLY_COPY_PASS_BY_REF. The maximum size (in bytes) to consider a `Copy` type for passing by value instead of by reference.
|
2020-02-05 02:06:34 +00:00
|
|
|
(trivial_copy_size_limit, "trivial_copy_size_limit": Option<u64>, None),
|
2020-10-08 05:17:32 +00:00
|
|
|
/// Lint: LARGE_TYPE_PASS_BY_MOVE. The minimum size (in bytes) to consider a type for passing by reference instead of by value.
|
|
|
|
(pass_by_value_size_limit, "pass_by_value_size_limit": u64, 256),
|
2019-01-13 15:19:02 +00:00
|
|
|
/// Lint: TOO_MANY_LINES. The maximum number of lines a function or method can have
|
2020-02-05 02:06:34 +00:00
|
|
|
(too_many_lines_threshold, "too_many_lines_threshold": u64, 100),
|
2020-03-23 21:07:46 +00:00
|
|
|
/// Lint: LARGE_STACK_ARRAYS, LARGE_CONST_ARRAYS. The maximum allowed size for arrays on the stack
|
2020-02-05 02:06:34 +00:00
|
|
|
(array_size_threshold, "array_size_threshold": u64, 512_000),
|
2020-01-23 14:52:41 +00:00
|
|
|
/// Lint: VEC_BOX. The size of the boxed type in bytes, where boxing in a `Vec` is allowed
|
2020-02-05 02:06:34 +00:00
|
|
|
(vec_box_size_threshold, "vec_box_size_threshold": u64, 4096),
|
2020-07-14 12:59:59 +00:00
|
|
|
/// Lint: TYPE_REPETITION_IN_BOUNDS. The maximum number of bounds a trait can have to be linted
|
|
|
|
(max_trait_bounds, "max_trait_bounds": u64, 3),
|
2020-02-01 23:49:52 +00:00
|
|
|
/// Lint: STRUCT_EXCESSIVE_BOOLS. The maximum number of bools a struct can have
|
|
|
|
(max_struct_bools, "max_struct_bools": u64, 3),
|
|
|
|
/// Lint: FN_PARAMS_EXCESSIVE_BOOLS. The maximum number of bools function parameters can have
|
|
|
|
(max_fn_params_bools, "max_fn_params_bools": u64, 3),
|
2020-05-11 18:23:47 +00:00
|
|
|
/// Lint: WILDCARD_IMPORTS. Whether to allow certain wildcard imports (prelude, super in tests).
|
|
|
|
(warn_on_all_wildcard_imports, "warn_on_all_wildcard_imports": bool, false),
|
2021-01-30 06:18:56 +00:00
|
|
|
/// Lint: DISALLOWED_METHOD. The list of disallowed methods, written as fully qualified paths.
|
2020-09-25 16:03:45 +00:00
|
|
|
(disallowed_methods, "disallowed_methods": Vec<String>, Vec::<String>::new()),
|
2020-12-04 21:26:47 +00:00
|
|
|
/// Lint: UNREADABLE_LITERAL. Should the fraction of a decimal be linted to include separators.
|
2020-12-05 20:59:53 +00:00
|
|
|
(unreadable_literal_lint_fractions, "unreadable_literal_lint_fractions": bool, true),
|
2021-02-24 23:10:06 +00:00
|
|
|
/// Lint: UPPER_CASE_ACRONYMS. Enables verbose mode. Triggers if there is more than one uppercase char next to each other
|
2021-02-24 22:20:49 +00:00
|
|
|
(upper_case_acronyms_aggressive, "upper_case_acronyms_aggressive": bool, false),
|
2021-02-06 15:51:51 +00:00
|
|
|
/// Lint: _CARGO_COMMON_METADATA. For internal testing only, ignores the current `publish` settings in the Cargo manifest.
|
2021-02-06 16:38:14 +00:00
|
|
|
(cargo_ignore_publish, "cargo_ignore_publish": bool, false),
|
2016-02-21 19:11:32 +00:00
|
|
|
}
|
|
|
|
|
2018-09-03 21:12:50 +00:00
|
|
|
impl Default for Conf {
|
2019-09-18 06:37:41 +00:00
|
|
|
#[must_use]
|
2018-09-26 09:32:05 +00:00
|
|
|
fn default() -> Self {
|
2018-09-03 21:12:50 +00:00
|
|
|
toml::from_str("").expect("we never error on empty config files")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-25 17:41:24 +00:00
|
|
|
/// Search for the configuration file.
|
2020-02-05 02:06:34 +00:00
|
|
|
pub fn lookup_conf_file() -> io::Result<Option<PathBuf>> {
|
2016-10-25 17:41:24 +00:00
|
|
|
/// Possible filename to search for.
|
2017-10-20 12:41:24 +00:00
|
|
|
const CONFIG_FILE_NAMES: [&str; 2] = [".clippy.toml", "clippy.toml"];
|
2016-10-25 17:41:24 +00:00
|
|
|
|
2019-01-27 00:11:30 +00:00
|
|
|
// Start looking for a config file in CLIPPY_CONF_DIR, or failing that, CARGO_MANIFEST_DIR.
|
|
|
|
// If neither of those exist, use ".".
|
2020-02-05 02:06:34 +00:00
|
|
|
let mut current = env::var_os("CLIPPY_CONF_DIR")
|
|
|
|
.or_else(|| env::var_os("CARGO_MANIFEST_DIR"))
|
|
|
|
.map_or_else(|| PathBuf::from("."), PathBuf::from);
|
2016-10-25 17:41:24 +00:00
|
|
|
loop {
|
|
|
|
for config_file_name in &CONFIG_FILE_NAMES {
|
|
|
|
let config_file = current.join(config_file_name);
|
|
|
|
match fs::metadata(&config_file) {
|
|
|
|
// Only return if it's a file to handle the unlikely situation of a directory named
|
|
|
|
// `clippy.toml`.
|
2020-02-05 02:06:34 +00:00
|
|
|
Ok(ref md) if !md.is_dir() => return Ok(Some(config_file)),
|
2016-10-25 17:41:24 +00:00
|
|
|
// Return the error if it's something other than `NotFound`; otherwise we didn't
|
|
|
|
// find the project file yet, and continue searching.
|
2020-02-05 02:06:34 +00:00
|
|
|
Err(e) if e.kind() != io::ErrorKind::NotFound => return Err(e),
|
|
|
|
_ => {},
|
2016-10-25 17:41:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the current directory has no parent, we're done searching.
|
|
|
|
if !current.pop() {
|
|
|
|
return Ok(None);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-09 14:02:48 +00:00
|
|
|
/// Produces a `Conf` filled with the default values and forwards the errors
|
|
|
|
///
|
|
|
|
/// Used internally for convenience
|
2017-05-09 13:23:38 +00:00
|
|
|
fn default(errors: Vec<Error>) -> (Conf, Vec<Error>) {
|
2018-09-03 21:12:50 +00:00
|
|
|
(Conf::default(), errors)
|
2017-05-09 13:23:38 +00:00
|
|
|
}
|
|
|
|
|
2016-10-25 17:41:24 +00:00
|
|
|
/// Read the `toml` configuration file.
|
|
|
|
///
|
2016-03-06 14:48:56 +00:00
|
|
|
/// In case of error, the function tries to continue as much as possible.
|
2020-02-05 02:06:34 +00:00
|
|
|
pub fn read(path: &Path) -> (Conf, Vec<Error>) {
|
|
|
|
let content = match fs::read_to_string(path) {
|
|
|
|
Ok(content) => content,
|
2017-05-09 13:23:38 +00:00
|
|
|
Err(err) => return default(vec![err.into()]),
|
2016-02-21 19:11:32 +00:00
|
|
|
};
|
|
|
|
|
2018-11-27 20:14:15 +00:00
|
|
|
assert!(ERRORS.lock().expect("no threading -> mutex always safe").is_empty());
|
2020-02-05 02:06:34 +00:00
|
|
|
match toml::from_str(&content) {
|
2019-02-23 01:19:50 +00:00
|
|
|
Ok(toml) => {
|
|
|
|
let mut errors = ERRORS.lock().expect("no threading -> mutex always safe").split_off(0);
|
|
|
|
|
|
|
|
let toml_ref: &Conf = &toml;
|
|
|
|
|
|
|
|
let cyc_field: Option<u64> = toml_ref.cyclomatic_complexity_threshold;
|
|
|
|
|
|
|
|
if cyc_field.is_some() {
|
|
|
|
let cyc_err = "found deprecated field `cyclomatic-complexity-threshold`. Please use `cognitive-complexity-threshold` instead.".to_string();
|
|
|
|
errors.push(Error::Toml(cyc_err));
|
|
|
|
}
|
|
|
|
|
|
|
|
(toml, errors)
|
|
|
|
},
|
2017-05-09 13:23:38 +00:00
|
|
|
Err(e) => {
|
2018-11-27 20:14:15 +00:00
|
|
|
let mut errors = ERRORS.lock().expect("no threading -> mutex always safe").split_off(0);
|
2017-05-09 13:23:38 +00:00
|
|
|
errors.push(Error::Toml(e.to_string()));
|
2019-02-23 01:19:50 +00:00
|
|
|
|
2017-05-09 13:23:38 +00:00
|
|
|
default(errors)
|
|
|
|
},
|
2016-02-21 19:11:32 +00:00
|
|
|
}
|
|
|
|
}
|