2018-09-11 23:34:52 +00:00
|
|
|
#![feature(tool_lints)]
|
|
|
|
#![allow(clippy::default_hash_types)]
|
2018-07-17 20:50:17 +00:00
|
|
|
extern crate regex;
|
|
|
|
#[macro_use]
|
|
|
|
extern crate lazy_static;
|
2018-09-02 07:45:13 +00:00
|
|
|
extern crate itertools;
|
2018-07-17 20:50:17 +00:00
|
|
|
|
|
|
|
use regex::Regex;
|
2018-09-02 07:45:13 +00:00
|
|
|
use itertools::Itertools;
|
|
|
|
use std::collections::HashMap;
|
2018-07-17 20:50:17 +00:00
|
|
|
use std::ffi::OsStr;
|
|
|
|
use std::fs;
|
|
|
|
use std::io::prelude::*;
|
|
|
|
|
|
|
|
lazy_static! {
|
2018-08-30 05:57:54 +00:00
|
|
|
static ref DEC_CLIPPY_LINT_RE: Regex = Regex::new(r#"(?x)
|
|
|
|
declare_clippy_lint!\s*[\{(]\s*
|
|
|
|
pub\s+(?P<name>[A-Z_][A-Z_0-9]*)\s*,\s*
|
|
|
|
(?P<cat>[a-z_]+)\s*,\s*
|
|
|
|
"(?P<desc>(?:[^"\\]+|\\(?s).(?-s))*)"\s*[})]
|
|
|
|
"#).unwrap();
|
|
|
|
static ref DEC_DEPRECATED_LINT_RE: Regex = Regex::new(r#"(?x)
|
|
|
|
declare_deprecated_lint!\s*[{(]\s*
|
|
|
|
pub\s+(?P<name>[A-Z_][A-Z_0-9]*)\s*,\s*
|
|
|
|
"(?P<desc>(?:[^"\\]+|\\(?s).(?-s))*)"\s*[})]
|
|
|
|
"#).unwrap();
|
2018-07-17 20:50:17 +00:00
|
|
|
static ref NL_ESCAPE_RE: Regex = Regex::new(r#"\\\n\s*"#).unwrap();
|
|
|
|
pub static ref DOCS_LINK: String = "https://rust-lang-nursery.github.io/rust-clippy/master/index.html".to_string();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, PartialEq, Debug)]
|
|
|
|
pub struct Lint {
|
|
|
|
pub name: String,
|
|
|
|
pub group: String,
|
|
|
|
pub desc: String,
|
|
|
|
pub deprecation: Option<String>,
|
|
|
|
pub module: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Lint {
|
|
|
|
pub fn new(name: &str, group: &str, desc: &str, deprecation: Option<&str>, module: &str) -> Lint {
|
|
|
|
Lint {
|
|
|
|
name: name.to_lowercase(),
|
|
|
|
group: group.to_string(),
|
|
|
|
desc: NL_ESCAPE_RE.replace(&desc.replace("\\\"", "\""), "").to_string(),
|
|
|
|
deprecation: deprecation.map(|d| d.to_string()),
|
|
|
|
module: module.to_string(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-05 11:34:28 +00:00
|
|
|
/// Returns all non-deprecated lints
|
|
|
|
pub fn active_lints(lints: &[Lint]) -> impl Iterator<Item=&Lint> {
|
|
|
|
lints.iter().filter(|l| l.deprecation.is_none())
|
2018-07-17 20:50:17 +00:00
|
|
|
}
|
|
|
|
|
2018-09-02 07:45:13 +00:00
|
|
|
/// Returns the lints in a HashMap, grouped by the different lint groups
|
|
|
|
pub fn by_lint_group(lints: &[Lint]) -> HashMap<String, Vec<Lint>> {
|
|
|
|
lints.iter().map(|lint| (lint.group.to_string(), lint.clone())).into_group_map()
|
2018-07-17 20:50:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-05 11:34:28 +00:00
|
|
|
pub fn gather_all() -> impl Iterator<Item=Lint> {
|
2018-09-06 06:19:09 +00:00
|
|
|
lint_files().flat_map(|f| gather_from_file(&f))
|
2018-07-17 20:50:17 +00:00
|
|
|
}
|
|
|
|
|
2018-09-06 06:19:09 +00:00
|
|
|
fn gather_from_file(dir_entry: &fs::DirEntry) -> impl Iterator<Item=Lint> {
|
2018-08-30 06:00:16 +00:00
|
|
|
let mut file = fs::File::open(dir_entry.path()).unwrap();
|
2018-07-17 20:50:17 +00:00
|
|
|
let mut content = String::new();
|
|
|
|
file.read_to_string(&mut content).unwrap();
|
2018-08-30 06:00:16 +00:00
|
|
|
parse_contents(&content, dir_entry.path().file_stem().unwrap().to_str().unwrap())
|
2018-07-17 20:50:17 +00:00
|
|
|
}
|
|
|
|
|
2018-09-05 11:34:28 +00:00
|
|
|
fn parse_contents(content: &str, filename: &str) -> impl Iterator<Item=Lint> {
|
2018-09-05 18:32:26 +00:00
|
|
|
let lints = DEC_CLIPPY_LINT_RE
|
|
|
|
.captures_iter(content)
|
|
|
|
.map(|m| Lint::new(&m["name"], &m["cat"], &m["desc"], None, filename));
|
|
|
|
let deprecated = DEC_DEPRECATED_LINT_RE
|
|
|
|
.captures_iter(content)
|
|
|
|
.map(|m| Lint::new( &m["name"], "Deprecated", &m["desc"], Some(&m["desc"]), filename));
|
|
|
|
// Removing the `.collect::<Vec<Lint>>().into_iter()` causes some lifetime issues due to the map
|
|
|
|
lints.chain(deprecated).collect::<Vec<Lint>>().into_iter()
|
2018-07-17 20:50:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Collects all .rs files in the `clippy_lints/src` directory
|
2018-09-05 11:34:28 +00:00
|
|
|
fn lint_files() -> impl Iterator<Item=fs::DirEntry> {
|
2018-09-05 18:32:26 +00:00
|
|
|
fs::read_dir("../clippy_lints/src")
|
|
|
|
.unwrap()
|
2018-07-17 20:50:17 +00:00
|
|
|
.filter_map(|f| f.ok())
|
|
|
|
.filter(|f| f.path().extension() == Some(OsStr::new("rs")))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_parse_contents() {
|
2018-09-05 11:34:28 +00:00
|
|
|
let result: Vec<Lint> = parse_contents(
|
2018-07-17 20:50:17 +00:00
|
|
|
r#"
|
|
|
|
declare_clippy_lint! {
|
|
|
|
pub PTR_ARG,
|
|
|
|
style,
|
|
|
|
"really long \
|
|
|
|
text"
|
|
|
|
}
|
|
|
|
|
|
|
|
declare_clippy_lint!{
|
|
|
|
pub DOC_MARKDOWN,
|
|
|
|
pedantic,
|
|
|
|
"single line"
|
|
|
|
}
|
|
|
|
|
|
|
|
/// some doc comment
|
|
|
|
declare_deprecated_lint! {
|
|
|
|
pub SHOULD_ASSERT_EQ,
|
|
|
|
"`assert!()` will be more flexible with RFC 2011"
|
|
|
|
}
|
|
|
|
"#,
|
2018-09-05 11:34:28 +00:00
|
|
|
"module_name").collect();
|
2018-07-17 20:50:17 +00:00
|
|
|
|
|
|
|
let expected = vec![
|
|
|
|
Lint::new("ptr_arg", "style", "really long text", None, "module_name"),
|
|
|
|
Lint::new("doc_markdown", "pedantic", "single line", None, "module_name"),
|
|
|
|
Lint::new(
|
|
|
|
"should_assert_eq",
|
|
|
|
"Deprecated",
|
|
|
|
"`assert!()` will be more flexible with RFC 2011",
|
|
|
|
Some("`assert!()` will be more flexible with RFC 2011"),
|
|
|
|
"module_name"
|
|
|
|
),
|
|
|
|
];
|
|
|
|
assert_eq!(expected, result);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_active_lints() {
|
|
|
|
let lints = vec![
|
|
|
|
Lint::new("should_assert_eq", "Deprecated", "abc", Some("Reason"), "module_name"),
|
|
|
|
Lint::new("should_assert_eq2", "Not Deprecated", "abc", None, "module_name")
|
|
|
|
];
|
|
|
|
let expected = vec![
|
|
|
|
Lint::new("should_assert_eq2", "Not Deprecated", "abc", None, "module_name")
|
|
|
|
];
|
2018-09-05 11:34:28 +00:00
|
|
|
assert_eq!(expected, Lint::active_lints(&lints).cloned().collect::<Vec<Lint>>());
|
2018-07-17 20:50:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2018-09-02 07:45:13 +00:00
|
|
|
fn test_by_lint_group() {
|
2018-07-17 20:50:17 +00:00
|
|
|
let lints = vec![
|
2018-09-02 07:45:13 +00:00
|
|
|
Lint::new("should_assert_eq", "group1", "abc", None, "module_name"),
|
|
|
|
Lint::new("should_assert_eq2", "group2", "abc", None, "module_name"),
|
|
|
|
Lint::new("incorrect_match", "group1", "abc", None, "module_name"),
|
2018-07-17 20:50:17 +00:00
|
|
|
];
|
2018-09-02 07:45:13 +00:00
|
|
|
let mut expected: HashMap<String, Vec<Lint>> = HashMap::new();
|
|
|
|
expected.insert("group1".to_string(), vec![
|
|
|
|
Lint::new("should_assert_eq", "group1", "abc", None, "module_name"),
|
|
|
|
Lint::new("incorrect_match", "group1", "abc", None, "module_name"),
|
|
|
|
]);
|
|
|
|
expected.insert("group2".to_string(), vec![
|
|
|
|
Lint::new("should_assert_eq2", "group2", "abc", None, "module_name")
|
|
|
|
]);
|
|
|
|
assert_eq!(expected, Lint::by_lint_group(&lints));
|
2018-07-17 20:50:17 +00:00
|
|
|
}
|