1 use crate::clippy_project_root;
3 use std::fmt::Write as _;
4 use std::fs::{self, OpenOptions};
5 use std::io::prelude::*;
6 use std::io::{self, ErrorKind};
7 use std::path::{Path, PathBuf};
13 project_root: PathBuf,
17 fn context<C: AsRef<str>>(self, text: C) -> Self;
20 impl<T> Context for io::Result<T> {
21 fn context<C: AsRef<str>>(self, text: C) -> Self {
25 let message = format!("{}: {}", text.as_ref(), e);
26 Err(io::Error::new(ErrorKind::Other, message))
32 /// Creates the files required to implement and test a new lint and runs `update_lints`.
36 /// This function errors out if the files couldn't be created or written to.
37 pub fn create(pass: Option<&str>, lint_name: Option<&str>, category: Option<&str>, msrv: bool) -> io::Result<()> {
39 pass: pass.expect("`pass` argument is validated by clap"),
40 name: lint_name.expect("`name` argument is validated by clap"),
41 category: category.expect("`category` argument is validated by clap"),
42 project_root: clippy_project_root(),
45 create_lint(&lint, msrv).context("Unable to create lint implementation")?;
46 create_test(&lint).context("Unable to create a test for the new lint")?;
47 add_lint(&lint, msrv).context("Unable to add lint to clippy_lints/src/lib.rs")
50 fn create_lint(lint: &LintData<'_>, enable_msrv: bool) -> io::Result<()> {
51 let lint_contents = get_lint_file_contents(lint, enable_msrv);
53 let lint_path = format!("clippy_lints/src/{}.rs", lint.name);
54 write_file(lint.project_root.join(&lint_path), lint_contents.as_bytes())
57 fn create_test(lint: &LintData<'_>) -> io::Result<()> {
58 fn create_project_layout<P: Into<PathBuf>>(lint_name: &str, location: P, case: &str, hint: &str) -> io::Result<()> {
59 let mut path = location.into().join(case);
60 fs::create_dir(&path)?;
61 write_file(path.join("Cargo.toml"), get_manifest_contents(lint_name, hint))?;
64 fs::create_dir(&path)?;
65 let header = format!("// compile-flags: --crate-name={}", lint_name);
66 write_file(path.join("main.rs"), get_test_file_contents(lint_name, Some(&header)))?;
71 if lint.category == "cargo" {
72 let relative_test_dir = format!("tests/ui-cargo/{}", lint.name);
73 let test_dir = lint.project_root.join(relative_test_dir);
74 fs::create_dir(&test_dir)?;
76 create_project_layout(lint.name, &test_dir, "fail", "Content that triggers the lint goes here")?;
77 create_project_layout(lint.name, &test_dir, "pass", "This file should not trigger the lint")
79 let test_path = format!("tests/ui/{}.rs", lint.name);
80 let test_contents = get_test_file_contents(lint.name, None);
81 write_file(lint.project_root.join(test_path), test_contents)
85 fn add_lint(lint: &LintData<'_>, enable_msrv: bool) -> io::Result<()> {
86 let path = "clippy_lints/src/lib.rs";
87 let mut lib_rs = fs::read_to_string(path).context("reading")?;
89 let comment_start = lib_rs.find("// add lints here,").expect("Couldn't find comment");
91 let new_lint = if enable_msrv {
93 "store.register_{lint_pass}_pass(move || Box::new({module_name}::{camel_name}::new(msrv)));\n ",
94 lint_pass = lint.pass,
95 module_name = lint.name,
96 camel_name = to_camel_case(lint.name),
100 "store.register_{lint_pass}_pass(|| Box::new({module_name}::{camel_name}));\n ",
101 lint_pass = lint.pass,
102 module_name = lint.name,
103 camel_name = to_camel_case(lint.name),
107 lib_rs.insert_str(comment_start, &new_lint);
109 fs::write(path, lib_rs).context("writing")
112 fn write_file<P: AsRef<Path>, C: AsRef<[u8]>>(path: P, contents: C) -> io::Result<()> {
113 fn inner(path: &Path, contents: &[u8]) -> io::Result<()> {
121 inner(path.as_ref(), contents.as_ref()).context(format!("writing to file: {}", path.as_ref().display()))
124 fn to_camel_case(name: &str) -> String {
130 [&s[0..1].to_uppercase(), &s[1..]].concat()
136 fn get_stabilisation_version() -> String {
137 fn parse_manifest(contents: &str) -> Option<String> {
138 let version = contents
140 .filter_map(|l| l.split_once('='))
141 .find_map(|(k, v)| (k.trim() == "version").then(|| v.trim()))?;
142 let Some(("0", version)) = version.get(1..version.len() - 1)?.split_once('.') else {
145 let (minor, patch) = version.split_once('.')?;
148 minor.parse::<u32>().ok()?,
149 patch.parse::<u32>().ok()?
152 let contents = fs::read_to_string("Cargo.toml").expect("Unable to read `Cargo.toml`");
153 parse_manifest(&contents).expect("Unable to find package version in `Cargo.toml`")
156 fn get_test_file_contents(lint_name: &str, header_commands: Option<&str>) -> String {
157 let mut contents = format!(
162 // test code goes here
168 if let Some(header) = header_commands {
169 contents = format!("{}\n{}", header, contents);
175 fn get_manifest_contents(lint_name: &str, hint: &str) -> String {
191 fn get_lint_file_contents(lint: &LintData<'_>, enable_msrv: bool) -> String {
192 let mut result = String::new();
194 let (pass_type, pass_lifetimes, pass_import, context_import) = match lint.pass {
195 "early" => ("EarlyLintPass", "", "use rustc_ast::ast::*;", "EarlyContext"),
196 "late" => ("LateLintPass", "<'_>", "use rustc_hir::*;", "LateContext"),
198 unreachable!("`pass_type` should only ever be `early` or `late`!");
202 let version = get_stabilisation_version();
203 let lint_name = lint.name;
204 let category = lint.category;
205 let name_camel = to_camel_case(lint.name);
206 let name_upper = lint_name.to_uppercase();
208 result.push_str(&if enable_msrv {
211 use clippy_utils::msrvs;
213 use rustc_lint::{{{context_import}, {pass_type}, LintContext}};
214 use rustc_semver::RustcVersion;
215 use rustc_session::{{declare_tool_lint, impl_lint_pass}};
218 pass_type = pass_type,
219 pass_import = pass_import,
220 context_import = context_import,
226 use rustc_lint::{{{context_import}, {pass_type}}};
227 use rustc_session::{{declare_lint_pass, declare_tool_lint}};
230 pass_import = pass_import,
231 pass_type = pass_type,
232 context_import = context_import
239 declare_clippy_lint! {{
242 /// ### Why is this bad?
246 /// // example code where clippy issues a warning
250 /// // example code which does not raise clippy warning
252 #[clippy::version = "{version}"]
255 "default lint description"
259 name_upper = name_upper,
263 result.push_str(&if enable_msrv {
266 pub struct {name_camel} {{
267 msrv: Option<RustcVersion>,
272 pub fn new(msrv: Option<RustcVersion>) -> Self {{
277 impl_lint_pass!({name_camel} => [{name_upper}]);
279 impl {pass_type}{pass_lifetimes} for {name_camel} {{
280 extract_msrv_attr!({context_import});
283 // TODO: Add MSRV level to `clippy_utils/src/msrvs.rs` if needed.
284 // TODO: Add MSRV test to `tests/ui/min_rust_version_attr.rs`.
285 // TODO: Update msrv config comment in `clippy_lints/src/utils/conf.rs`
287 pass_type = pass_type,
288 pass_lifetimes = pass_lifetimes,
289 name_upper = name_upper,
290 name_camel = name_camel,
291 context_import = context_import,
296 declare_lint_pass!({name_camel} => [{name_upper}]);
298 impl {pass_type}{pass_lifetimes} for {name_camel} {{}}
300 pass_type = pass_type,
301 pass_lifetimes = pass_lifetimes,
302 name_upper = name_upper,
303 name_camel = name_camel,
311 fn test_camel_case() {
313 let s2 = to_camel_case(s);
314 assert_eq!(s2, "ALint");
316 let name = "a_really_long_new_lint";
317 let name2 = to_camel_case(name);
318 assert_eq!(name2, "AReallyLongNewLint");
320 let name3 = "lint__name";
321 let name4 = to_camel_case(name3);
322 assert_eq!(name4, "LintName");