Use indoc for formatting

This commit is contained in:
Michael Wright 2021-10-09 05:58:05 +02:00
parent b8782257ae
commit 7d9b21b90b
2 changed files with 74 additions and 67 deletions

View file

@ -6,6 +6,7 @@ edition = "2021"
[dependencies] [dependencies]
bytecount = "0.6" bytecount = "0.6"
clap = "2.33" clap = "2.33"
indoc = "1.0"
itertools = "0.10" itertools = "0.10"
opener = "0.5" opener = "0.5"
regex = "1.5" regex = "1.5"

View file

@ -1,4 +1,5 @@
use crate::clippy_project_root; use crate::clippy_project_root;
use indoc::indoc;
use std::fs::{self, OpenOptions}; use std::fs::{self, OpenOptions};
use std::io::prelude::*; use std::io::prelude::*;
use std::io::{self, ErrorKind}; use std::io::{self, ErrorKind};
@ -105,12 +106,13 @@ fn to_camel_case(name: &str) -> String {
fn get_test_file_contents(lint_name: &str, header_commands: Option<&str>) -> String { fn get_test_file_contents(lint_name: &str, header_commands: Option<&str>) -> String {
let mut contents = format!( let mut contents = format!(
"#![warn(clippy::{})] indoc! {"
#![warn(clippy::{})]
fn main() {{ fn main() {{
// test code goes here // test code goes here
}} }}
", "},
lint_name lint_name
); );
@ -123,16 +125,16 @@ fn main() {{
fn get_manifest_contents(lint_name: &str, hint: &str) -> String { fn get_manifest_contents(lint_name: &str, hint: &str) -> String {
format!( format!(
r#" indoc! {r#"
# {} # {}
[package] [package]
name = "{}" name = "{}"
version = "0.1.0" version = "0.1.0"
publish = false publish = false
[workspace] [workspace]
"#, "#},
hint, lint_name hint, lint_name
) )
} }
@ -156,24 +158,26 @@ fn get_lint_file_contents(lint: &LintData<'_>, enable_msrv: bool) -> String {
result.push_str(&if enable_msrv { result.push_str(&if enable_msrv {
format!( format!(
"use clippy_utils::msrvs; indoc! {"
{pass_import} use clippy_utils::msrvs;
use rustc_lint::{{{context_import}, {pass_type}, LintContext}}; {pass_import}
use rustc_semver::RustcVersion; use rustc_lint::{{{context_import}, {pass_type}, LintContext}};
use rustc_session::{{declare_tool_lint, impl_lint_pass}}; use rustc_semver::RustcVersion;
use rustc_session::{{declare_tool_lint, impl_lint_pass}};
", "},
pass_type = pass_type, pass_type = pass_type,
pass_import = pass_import, pass_import = pass_import,
context_import = context_import, context_import = context_import,
) )
} else { } else {
format!( format!(
"{pass_import} indoc! {"
use rustc_lint::{{{context_import}, {pass_type}}}; {pass_import}
use rustc_session::{{declare_lint_pass, declare_tool_lint}}; use rustc_lint::{{{context_import}, {pass_type}}};
use rustc_session::{{declare_lint_pass, declare_tool_lint}};
", "},
pass_import = pass_import, pass_import = pass_import,
pass_type = pass_type, pass_type = pass_type,
context_import = context_import context_import = context_import
@ -181,53 +185,55 @@ use rustc_session::{{declare_lint_pass, declare_tool_lint}};
}); });
result.push_str(&format!( result.push_str(&format!(
"declare_clippy_lint! {{ indoc! {"
/// ### What it does declare_clippy_lint! {{
/// /// ### What it does
/// ### Why is this bad? ///
/// /// ### Why is this bad?
/// ### Example ///
/// ```rust /// ### Example
/// // example code where clippy issues a warning /// ```rust
/// ``` /// // example code where clippy issues a warning
/// Use instead: /// ```
/// ```rust /// Use instead:
/// // example code which does not raise clippy warning /// ```rust
/// ``` /// // example code which does not raise clippy warning
pub {name_upper}, /// ```
{category}, pub {name_upper},
\"default lint description\" {category},
}}", \"default lint description\"
}}
"},
name_upper = name_upper, name_upper = name_upper,
category = category, category = category,
)); ));
result.push_str(&if enable_msrv { result.push_str(&if enable_msrv {
format!( format!(
" indoc! {"
pub struct {name_camel} {{ pub struct {name_camel} {{
msrv: Option<RustcVersion>, msrv: Option<RustcVersion>,
}} }}
impl {name_camel} {{ impl {name_camel} {{
#[must_use] #[must_use]
pub fn new(msrv: Option<RustcVersion>) -> Self {{ pub fn new(msrv: Option<RustcVersion>) -> Self {{
Self {{ msrv }} Self {{ msrv }}
}} }}
}} }}
impl_lint_pass!({name_camel} => [{name_upper}]); impl_lint_pass!({name_camel} => [{name_upper}]);
impl {pass_type}{pass_lifetimes} for {name_camel} {{ impl {pass_type}{pass_lifetimes} for {name_camel} {{
extract_msrv_attr!({context_import}); extract_msrv_attr!({context_import});
}} }}
// TODO: Register the lint pass in `clippy_lints/src/lib.rs`, // TODO: Register the lint pass in `clippy_lints/src/lib.rs`,
// e.g. store.register_{pass_name}_pass(move || Box::new({module_name}::{name_camel}::new(msrv))); // e.g. store.register_{pass_name}_pass(move || Box::new({module_name}::{name_camel}::new(msrv)));
// TODO: Add MSRV level to `clippy_utils/src/msrvs.rs` if needed. // TODO: Add MSRV level to `clippy_utils/src/msrvs.rs` if needed.
// TODO: Add MSRV test to `tests/ui/min_rust_version_attr.rs`. // TODO: Add MSRV test to `tests/ui/min_rust_version_attr.rs`.
// TODO: Update msrv config comment in `clippy_lints/src/utils/conf.rs` // TODO: Update msrv config comment in `clippy_lints/src/utils/conf.rs`
", "},
pass_type = pass_type, pass_type = pass_type,
pass_lifetimes = pass_lifetimes, pass_lifetimes = pass_lifetimes,
pass_name = pass_name, pass_name = pass_name,
@ -238,14 +244,14 @@ impl {pass_type}{pass_lifetimes} for {name_camel} {{
) )
} else { } else {
format!( format!(
" indoc! {"
declare_lint_pass!({name_camel} => [{name_upper}]); declare_lint_pass!({name_camel} => [{name_upper}]);
impl {pass_type}{pass_lifetimes} for {name_camel} {{}} impl {pass_type}{pass_lifetimes} for {name_camel} {{}}
// //
// TODO: Register the lint pass in `clippy_lints/src/lib.rs`, // TODO: Register the lint pass in `clippy_lints/src/lib.rs`,
// e.g. store.register_{pass_name}_pass(|| Box::new({module_name}::{name_camel})); // e.g. store.register_{pass_name}_pass(|| Box::new({module_name}::{name_camel}));
", "},
pass_type = pass_type, pass_type = pass_type,
pass_lifetimes = pass_lifetimes, pass_lifetimes = pass_lifetimes,
pass_name = pass_name, pass_name = pass_name,