-/// This function errors, if the files couldn't be created
-pub fn create(pass: Option<&str>, lint_name: Option<&str>, category: Option<&str>) -> Result<(), io::Error> {
- let pass = pass.expect("`pass` argument is validated by clap");
- let lint_name = lint_name.expect("`name` argument is validated by clap");
- let category = category.expect("`category` argument is validated by clap");
-
- match open_files(lint_name) {
- Ok((mut test_file, mut lint_file)) => {
- let (pass_type, pass_lifetimes, pass_import, context_import) = match pass {
- "early" => ("EarlyLintPass", "", "use rustc_ast::ast::*;", "EarlyContext"),
- "late" => ("LateLintPass", "<'_, '_>", "use rustc_hir::*;", "LateContext"),
- _ => {
- unreachable!("`pass_type` should only ever be `early` or `late`!");
- },
- };
-
- let camel_case_name = to_camel_case(lint_name);
-
- if let Err(e) = test_file.write_all(get_test_file_contents(lint_name).as_bytes()) {
- return Err(io::Error::new(
- ErrorKind::Other,
- format!("Could not write to test file: {}", e),
- ));
- };
-
- if let Err(e) = lint_file.write_all(
- get_lint_file_contents(
- pass_type,
- pass_lifetimes,
- lint_name,
- &camel_case_name,
- category,
- pass_import,
- context_import,
- )
- .as_bytes(),
- ) {
- return Err(io::Error::new(
- ErrorKind::Other,
- format!("Could not write to lint file: {}", e),
- ));
- }
- Ok(())
+/// This function errors out if the files couldn't be created or written to.
+pub fn create(pass: Option<&str>, lint_name: Option<&str>, category: Option<&str>) -> io::Result<()> {
+ let lint = LintData {
+ pass: pass.expect("`pass` argument is validated by clap"),
+ name: lint_name.expect("`name` argument is validated by clap"),
+ category: category.expect("`category` argument is validated by clap"),
+ project_root: clippy_project_root(),
+ };
+
+ create_lint(&lint).context("Unable to create lint implementation")?;
+ create_test(&lint).context("Unable to create a test for the new lint")
+}
+
+fn create_lint(lint: &LintData) -> io::Result<()> {
+ let (pass_type, pass_lifetimes, pass_import, context_import) = match lint.pass {
+ "early" => ("EarlyLintPass", "", "use rustc_ast::ast::*;", "EarlyContext"),
+ "late" => ("LateLintPass", "<'_, '_>", "use rustc_hir::*;", "LateContext"),
+ _ => {
+ unreachable!("`pass_type` should only ever be `early` or `late`!");