1 use clippy_dev::clippy_project_root;
2 use std::fs::{File, OpenOptions};
4 use std::io::prelude::*;
5 use std::io::ErrorKind;
8 pub fn create(pass: Option<&str>, lint_name: Option<&str>, category: Option<&str>) -> Result<(), io::Error> {
9 let pass = pass.expect("`pass` argument is validated by clap");
10 let lint_name = lint_name.expect("`name` argument is validated by clap");
11 let category = category.expect("`category` argument is validated by clap");
13 match open_files(lint_name) {
14 Ok((mut test_file, mut lint_file)) => {
15 let (pass_type, pass_lifetimes, pass_import, context_import) = match pass {
16 "early" => ("EarlyLintPass", "", "use rustc_ast::ast::*;", "EarlyContext"),
17 "late" => ("LateLintPass", "<'_, '_>", "use rustc_hir::*;", "LateContext"),
19 unreachable!("`pass_type` should only ever be `early` or `late`!");
23 let camel_case_name = to_camel_case(lint_name);
25 if let Err(e) = test_file.write_all(get_test_file_contents(lint_name).as_bytes()) {
26 return Err(io::Error::new(
28 format!("Could not write to test file: {}", e),
32 if let Err(e) = lint_file.write_all(
33 get_lint_file_contents(
44 return Err(io::Error::new(
46 format!("Could not write to lint file: {}", e),
51 Err(e) => Err(io::Error::new(
53 format!("Unable to create lint: {}", e),
58 fn open_files(lint_name: &str) -> Result<(File, File), io::Error> {
59 let project_root = clippy_project_root();
61 let test_file_path = project_root.join("tests").join("ui").join(format!("{}.rs", lint_name));
62 let lint_file_path = project_root
65 .join(format!("{}.rs", lint_name));
67 if Path::new(&test_file_path).exists() {
68 return Err(io::Error::new(
69 ErrorKind::AlreadyExists,
70 format!("test file {:?} already exists", test_file_path),
73 if Path::new(&lint_file_path).exists() {
74 return Err(io::Error::new(
75 ErrorKind::AlreadyExists,
76 format!("lint file {:?} already exists", lint_file_path),
80 let test_file = OpenOptions::new().write(true).create_new(true).open(test_file_path)?;
81 let lint_file = OpenOptions::new().write(true).create_new(true).open(lint_file_path)?;
83 Ok((test_file, lint_file))
86 fn to_camel_case(name: &str) -> String {
92 [&s[0..1].to_uppercase(), &s[1..]].concat()
98 fn get_test_file_contents(lint_name: &str) -> String {
100 "#![warn(clippy::{})]
103 // test code goes here
110 fn get_lint_file_contents(
112 pass_lifetimes: &str,
114 camel_case_name: &str,
117 context_import: &str,
120 "use rustc_lint::{{{type}, {context_import}}};
121 use rustc_session::{{declare_lint_pass, declare_tool_lint}};
124 declare_clippy_lint! {{
125 /// **What it does:**
127 /// **Why is this bad?**
129 /// **Known problems:** None.
134 /// // example code where clippy issues a warning
138 /// // example code which does not raise clippy warning
142 \"default lint description\"
145 declare_lint_pass!({name_camel} => [{name_upper}]);
147 impl {type}{lifetimes} for {name_camel} {{}}
150 lifetimes=pass_lifetimes,
151 name_upper=lint_name.to_uppercase(),
152 name_camel=camel_case_name,
154 pass_import=pass_import,
155 context_import=context_import
160 fn test_camel_case() {
162 let s2 = to_camel_case(s);
163 assert_eq!(s2, "ALint");
165 let name = "a_really_long_new_lint";
166 let name2 = to_camel_case(name);
167 assert_eq!(name2, "AReallyLongNewLint");
169 let name3 = "lint__name";
170 let name4 = to_camel_case(name3);
171 assert_eq!(name4, "LintName");