Skip to content

Commit

Permalink
Auto merge of #9527 - nyurik:inl2, r=llogiq
Browse files Browse the repository at this point in the history
fallout2: rework clippy_dev & _lints fmt inlining

Continuing #9525 -- a few more inlining, but this time with some code changes to simplify format strings:

* Inline format args where possible
* simplify a few complex macros into format str
* use formatdoc!() instead format!(indoc!(...))

changelog: none

cc: `@llogiq`
  • Loading branch information
bors committed Sep 24, 2022
2 parents c2d4266 + cc6b375 commit 8b1ad17
Show file tree
Hide file tree
Showing 9 changed files with 181 additions and 266 deletions.
6 changes: 3 additions & 3 deletions clippy_dev/src/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,16 @@ pub fn run(check: bool, verbose: bool) {
fn output_err(err: CliError) {
match err {
CliError::CommandFailed(command, stderr) => {
eprintln!("error: A command failed! `{}`\nstderr: {}", command, stderr);
eprintln!("error: A command failed! `{command}`\nstderr: {stderr}");
},
CliError::IoError(err) => {
eprintln!("error: {}", err);
eprintln!("error: {err}");
},
CliError::RustfmtNotInstalled => {
eprintln!("error: rustfmt nightly is not installed.");
},
CliError::WalkDirError(err) => {
eprintln!("error: {}", err);
eprintln!("error: {err}");
},
CliError::IntellijSetupActive => {
eprintln!(
Expand Down
167 changes: 74 additions & 93 deletions clippy_dev/src/new_lint.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::clippy_project_root;
use indoc::{indoc, writedoc};
use indoc::{formatdoc, writedoc};
use std::fmt::Write as _;
use std::fs::{self, OpenOptions};
use std::io::prelude::*;
Expand All @@ -23,7 +23,7 @@ impl<T> Context for io::Result<T> {
match self {
Ok(t) => Ok(t),
Err(e) => {
let message = format!("{}: {}", text.as_ref(), e);
let message = format!("{}: {e}", text.as_ref());
Err(io::Error::new(ErrorKind::Other, message))
},
}
Expand Down Expand Up @@ -72,7 +72,7 @@ fn create_lint(lint: &LintData<'_>, enable_msrv: bool) -> io::Result<()> {
let lint_contents = get_lint_file_contents(lint, enable_msrv);
let lint_path = format!("clippy_lints/src/{}.rs", lint.name);
write_file(lint.project_root.join(&lint_path), lint_contents.as_bytes())?;
println!("Generated lint file: `{}`", lint_path);
println!("Generated lint file: `{lint_path}`");

Ok(())
}
Expand All @@ -86,7 +86,7 @@ fn create_test(lint: &LintData<'_>) -> io::Result<()> {

path.push("src");
fs::create_dir(&path)?;
let header = format!("// compile-flags: --crate-name={}", lint_name);
let header = format!("// compile-flags: --crate-name={lint_name}");
write_file(path.join("main.rs"), get_test_file_contents(lint_name, Some(&header)))?;

Ok(())
Expand All @@ -106,7 +106,7 @@ fn create_test(lint: &LintData<'_>) -> io::Result<()> {
let test_contents = get_test_file_contents(lint.name, None);
write_file(lint.project_root.join(&test_path), test_contents)?;

println!("Generated test file: `{}`", test_path);
println!("Generated test file: `{test_path}`");
}

Ok(())
Expand Down Expand Up @@ -184,38 +184,36 @@ pub(crate) fn get_stabilization_version() -> String {
}

fn get_test_file_contents(lint_name: &str, header_commands: Option<&str>) -> String {
let mut contents = format!(
indoc! {"
#![allow(unused)]
#![warn(clippy::{})]
fn main() {{
// test code goes here
}}
"},
lint_name
let mut contents = formatdoc!(
r#"
#![allow(unused)]
#![warn(clippy::{lint_name})]
fn main() {{
// test code goes here
}}
"#
);

if let Some(header) = header_commands {
contents = format!("{}\n{}", header, contents);
contents = format!("{header}\n{contents}");
}

contents
}

fn get_manifest_contents(lint_name: &str, hint: &str) -> String {
format!(
indoc! {r#"
# {}
[package]
name = "{}"
version = "0.1.0"
publish = false
[workspace]
"#},
hint, lint_name
formatdoc!(
r#"
# {hint}
[package]
name = "{lint_name}"
version = "0.1.0"
publish = false
[workspace]
"#
)
}

Expand All @@ -236,85 +234,70 @@ fn get_lint_file_contents(lint: &LintData<'_>, enable_msrv: bool) -> String {
let name_upper = lint_name.to_uppercase();

result.push_str(&if enable_msrv {
format!(
indoc! {"
use clippy_utils::msrvs;
{pass_import}
use rustc_lint::{{{context_import}, {pass_type}, LintContext}};
use rustc_semver::RustcVersion;
use rustc_session::{{declare_tool_lint, impl_lint_pass}};
formatdoc!(
r#"
use clippy_utils::msrvs;
{pass_import}
use rustc_lint::{{{context_import}, {pass_type}, LintContext}};
use rustc_semver::RustcVersion;
use rustc_session::{{declare_tool_lint, impl_lint_pass}};
"},
pass_type = pass_type,
pass_import = pass_import,
context_import = context_import,
"#
)
} else {
format!(
indoc! {"
{pass_import}
use rustc_lint::{{{context_import}, {pass_type}}};
use rustc_session::{{declare_lint_pass, declare_tool_lint}};
"},
pass_import = pass_import,
pass_type = pass_type,
context_import = context_import
formatdoc!(
r#"
{pass_import}
use rustc_lint::{{{context_import}, {pass_type}}};
use rustc_session::{{declare_lint_pass, declare_tool_lint}};
"#
)
});

let _ = write!(result, "{}", get_lint_declaration(&name_upper, category));

result.push_str(&if enable_msrv {
format!(
indoc! {"
pub struct {name_camel} {{
msrv: Option<RustcVersion>,
}}
formatdoc!(
r#"
pub struct {name_camel} {{
msrv: Option<RustcVersion>,
}}
impl {name_camel} {{
#[must_use]
pub fn new(msrv: Option<RustcVersion>) -> Self {{
Self {{ msrv }}
}}
impl {name_camel} {{
#[must_use]
pub fn new(msrv: Option<RustcVersion>) -> Self {{
Self {{ msrv }}
}}
}}
impl_lint_pass!({name_camel} => [{name_upper}]);
impl_lint_pass!({name_camel} => [{name_upper}]);
impl {pass_type}{pass_lifetimes} for {name_camel} {{
extract_msrv_attr!({context_import});
}}
impl {pass_type}{pass_lifetimes} for {name_camel} {{
extract_msrv_attr!({context_import});
}}
// 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: Update msrv config comment in `clippy_lints/src/utils/conf.rs`
"},
pass_type = pass_type,
pass_lifetimes = pass_lifetimes,
name_upper = name_upper,
name_camel = name_camel,
context_import = context_import,
// 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: Update msrv config comment in `clippy_lints/src/utils/conf.rs`
"#
)
} else {
format!(
indoc! {"
declare_lint_pass!({name_camel} => [{name_upper}]);
formatdoc!(
r#"
declare_lint_pass!({name_camel} => [{name_upper}]);
impl {pass_type}{pass_lifetimes} for {name_camel} {{}}
"},
pass_type = pass_type,
pass_lifetimes = pass_lifetimes,
name_upper = name_upper,
name_camel = name_camel,
impl {pass_type}{pass_lifetimes} for {name_camel} {{}}
"#
)
});

result
}

fn get_lint_declaration(name_upper: &str, category: &str) -> String {
format!(
indoc! {r#"
formatdoc!(
r#"
declare_clippy_lint! {{
/// ### What it does
///
Expand All @@ -328,15 +311,13 @@ fn get_lint_declaration(name_upper: &str, category: &str) -> String {
/// ```rust
/// // example code which does not raise clippy warning
/// ```
#[clippy::version = "{version}"]
#[clippy::version = "{}"]
pub {name_upper},
{category},
"default lint description"
}}
"#},
version = get_stabilization_version(),
name_upper = name_upper,
category = category,
"#,
get_stabilization_version(),
)
}

Expand All @@ -350,7 +331,7 @@ fn create_lint_for_ty(lint: &LintData<'_>, enable_msrv: bool, ty: &str) -> io::R
_ => {},
}

let ty_dir = lint.project_root.join(format!("clippy_lints/src/{}", ty));
let ty_dir = lint.project_root.join(format!("clippy_lints/src/{ty}"));
assert!(
ty_dir.exists() && ty_dir.is_dir(),
"Directory `{}` does not exist!",
Expand Down Expand Up @@ -410,10 +391,10 @@ fn create_lint_for_ty(lint: &LintData<'_>, enable_msrv: bool, ty: &str) -> io::R
}

write_file(lint_file_path.as_path(), lint_file_contents)?;
println!("Generated lint file: `clippy_lints/src/{}/{}.rs`", ty, lint.name);
println!("Generated lint file: `clippy_lints/src/{ty}/{}.rs`", lint.name);
println!(
"Be sure to add a call to `{}::check` in `clippy_lints/src/{}/mod.rs`!",
lint.name, ty
"Be sure to add a call to `{}::check` in `clippy_lints/src/{ty}/mod.rs`!",
lint.name
);

Ok(())
Expand Down Expand Up @@ -540,7 +521,7 @@ fn setup_mod_file(path: &Path, lint: &LintData<'_>) -> io::Result<&'static str>
.chain(std::iter::once(&*lint_name_upper))
.filter(|s| !s.is_empty())
{
let _ = write!(new_arr_content, "\n {},", ident);
let _ = write!(new_arr_content, "\n {ident},");
}
new_arr_content.push('\n');

Expand Down
4 changes: 2 additions & 2 deletions clippy_dev/src/serve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ use std::time::{Duration, SystemTime};
/// Panics if the python commands could not be spawned
pub fn run(port: u16, lint: Option<&String>) -> ! {
let mut url = Some(match lint {
None => format!("http://localhost:{}", port),
Some(lint) => format!("http://localhost:{}/#{}", port, lint),
None => format!("http://localhost:{port}"),
Some(lint) => format!("http://localhost:{port}/#{lint}"),
});

loop {
Expand Down
7 changes: 2 additions & 5 deletions clippy_dev/src/setup/git_hook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,7 @@ pub fn install_hook(force_override: bool) {
println!("info: the hook can be removed with `cargo dev remove git-hook`");
println!("git hook successfully installed");
},
Err(err) => eprintln!(
"error: unable to copy `{}` to `{}` ({})",
HOOK_SOURCE_FILE, HOOK_TARGET_FILE, err
),
Err(err) => eprintln!("error: unable to copy `{HOOK_SOURCE_FILE}` to `{HOOK_TARGET_FILE}` ({err})"),
}
}

Expand Down Expand Up @@ -77,7 +74,7 @@ pub fn remove_hook() {

fn delete_git_hook_file(path: &Path) -> bool {
if let Err(err) = fs::remove_file(path) {
eprintln!("error: unable to delete existing pre-commit git hook ({})", err);
eprintln!("error: unable to delete existing pre-commit git hook ({err})");
false
} else {
true
Expand Down
Loading

0 comments on commit 8b1ad17

Please sign in to comment.