Skip to content

Commit 8b1ad17

Browse files
committed
Auto merge of #9527 - nyurik:inl2, r=llogiq
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`
2 parents c2d4266 + cc6b375 commit 8b1ad17

File tree

9 files changed

+181
-266
lines changed

9 files changed

+181
-266
lines changed

clippy_dev/src/fmt.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -82,16 +82,16 @@ pub fn run(check: bool, verbose: bool) {
8282
fn output_err(err: CliError) {
8383
match err {
8484
CliError::CommandFailed(command, stderr) => {
85-
eprintln!("error: A command failed! `{}`\nstderr: {}", command, stderr);
85+
eprintln!("error: A command failed! `{command}`\nstderr: {stderr}");
8686
},
8787
CliError::IoError(err) => {
88-
eprintln!("error: {}", err);
88+
eprintln!("error: {err}");
8989
},
9090
CliError::RustfmtNotInstalled => {
9191
eprintln!("error: rustfmt nightly is not installed.");
9292
},
9393
CliError::WalkDirError(err) => {
94-
eprintln!("error: {}", err);
94+
eprintln!("error: {err}");
9595
},
9696
CliError::IntellijSetupActive => {
9797
eprintln!(

clippy_dev/src/new_lint.rs

+74-93
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::clippy_project_root;
2-
use indoc::{indoc, writedoc};
2+
use indoc::{formatdoc, writedoc};
33
use std::fmt::Write as _;
44
use std::fs::{self, OpenOptions};
55
use std::io::prelude::*;
@@ -23,7 +23,7 @@ impl<T> Context for io::Result<T> {
2323
match self {
2424
Ok(t) => Ok(t),
2525
Err(e) => {
26-
let message = format!("{}: {}", text.as_ref(), e);
26+
let message = format!("{}: {e}", text.as_ref());
2727
Err(io::Error::new(ErrorKind::Other, message))
2828
},
2929
}
@@ -72,7 +72,7 @@ fn create_lint(lint: &LintData<'_>, enable_msrv: bool) -> io::Result<()> {
7272
let lint_contents = get_lint_file_contents(lint, enable_msrv);
7373
let lint_path = format!("clippy_lints/src/{}.rs", lint.name);
7474
write_file(lint.project_root.join(&lint_path), lint_contents.as_bytes())?;
75-
println!("Generated lint file: `{}`", lint_path);
75+
println!("Generated lint file: `{lint_path}`");
7676

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

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

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

109-
println!("Generated test file: `{}`", test_path);
109+
println!("Generated test file: `{test_path}`");
110110
}
111111

112112
Ok(())
@@ -184,38 +184,36 @@ pub(crate) fn get_stabilization_version() -> String {
184184
}
185185

186186
fn get_test_file_contents(lint_name: &str, header_commands: Option<&str>) -> String {
187-
let mut contents = format!(
188-
indoc! {"
189-
#![allow(unused)]
190-
#![warn(clippy::{})]
191-
192-
fn main() {{
193-
// test code goes here
194-
}}
195-
"},
196-
lint_name
187+
let mut contents = formatdoc!(
188+
r#"
189+
#![allow(unused)]
190+
#![warn(clippy::{lint_name})]
191+
192+
fn main() {{
193+
// test code goes here
194+
}}
195+
"#
197196
);
198197

199198
if let Some(header) = header_commands {
200-
contents = format!("{}\n{}", header, contents);
199+
contents = format!("{header}\n{contents}");
201200
}
202201

203202
contents
204203
}
205204

206205
fn get_manifest_contents(lint_name: &str, hint: &str) -> String {
207-
format!(
208-
indoc! {r#"
209-
# {}
210-
211-
[package]
212-
name = "{}"
213-
version = "0.1.0"
214-
publish = false
215-
216-
[workspace]
217-
"#},
218-
hint, lint_name
206+
formatdoc!(
207+
r#"
208+
# {hint}
209+
210+
[package]
211+
name = "{lint_name}"
212+
version = "0.1.0"
213+
publish = false
214+
215+
[workspace]
216+
"#
219217
)
220218
}
221219

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

238236
result.push_str(&if enable_msrv {
239-
format!(
240-
indoc! {"
241-
use clippy_utils::msrvs;
242-
{pass_import}
243-
use rustc_lint::{{{context_import}, {pass_type}, LintContext}};
244-
use rustc_semver::RustcVersion;
245-
use rustc_session::{{declare_tool_lint, impl_lint_pass}};
237+
formatdoc!(
238+
r#"
239+
use clippy_utils::msrvs;
240+
{pass_import}
241+
use rustc_lint::{{{context_import}, {pass_type}, LintContext}};
242+
use rustc_semver::RustcVersion;
243+
use rustc_session::{{declare_tool_lint, impl_lint_pass}};
246244
247-
"},
248-
pass_type = pass_type,
249-
pass_import = pass_import,
250-
context_import = context_import,
245+
"#
251246
)
252247
} else {
253-
format!(
254-
indoc! {"
255-
{pass_import}
256-
use rustc_lint::{{{context_import}, {pass_type}}};
257-
use rustc_session::{{declare_lint_pass, declare_tool_lint}};
258-
259-
"},
260-
pass_import = pass_import,
261-
pass_type = pass_type,
262-
context_import = context_import
248+
formatdoc!(
249+
r#"
250+
{pass_import}
251+
use rustc_lint::{{{context_import}, {pass_type}}};
252+
use rustc_session::{{declare_lint_pass, declare_tool_lint}};
253+
254+
"#
263255
)
264256
});
265257

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

268260
result.push_str(&if enable_msrv {
269-
format!(
270-
indoc! {"
271-
pub struct {name_camel} {{
272-
msrv: Option<RustcVersion>,
273-
}}
261+
formatdoc!(
262+
r#"
263+
pub struct {name_camel} {{
264+
msrv: Option<RustcVersion>,
265+
}}
274266
275-
impl {name_camel} {{
276-
#[must_use]
277-
pub fn new(msrv: Option<RustcVersion>) -> Self {{
278-
Self {{ msrv }}
279-
}}
267+
impl {name_camel} {{
268+
#[must_use]
269+
pub fn new(msrv: Option<RustcVersion>) -> Self {{
270+
Self {{ msrv }}
280271
}}
272+
}}
281273
282-
impl_lint_pass!({name_camel} => [{name_upper}]);
274+
impl_lint_pass!({name_camel} => [{name_upper}]);
283275
284-
impl {pass_type}{pass_lifetimes} for {name_camel} {{
285-
extract_msrv_attr!({context_import});
286-
}}
276+
impl {pass_type}{pass_lifetimes} for {name_camel} {{
277+
extract_msrv_attr!({context_import});
278+
}}
287279
288-
// TODO: Add MSRV level to `clippy_utils/src/msrvs.rs` if needed.
289-
// TODO: Add MSRV test to `tests/ui/min_rust_version_attr.rs`.
290-
// TODO: Update msrv config comment in `clippy_lints/src/utils/conf.rs`
291-
"},
292-
pass_type = pass_type,
293-
pass_lifetimes = pass_lifetimes,
294-
name_upper = name_upper,
295-
name_camel = name_camel,
296-
context_import = context_import,
280+
// TODO: Add MSRV level to `clippy_utils/src/msrvs.rs` if needed.
281+
// TODO: Add MSRV test to `tests/ui/min_rust_version_attr.rs`.
282+
// TODO: Update msrv config comment in `clippy_lints/src/utils/conf.rs`
283+
"#
297284
)
298285
} else {
299-
format!(
300-
indoc! {"
301-
declare_lint_pass!({name_camel} => [{name_upper}]);
286+
formatdoc!(
287+
r#"
288+
declare_lint_pass!({name_camel} => [{name_upper}]);
302289
303-
impl {pass_type}{pass_lifetimes} for {name_camel} {{}}
304-
"},
305-
pass_type = pass_type,
306-
pass_lifetimes = pass_lifetimes,
307-
name_upper = name_upper,
308-
name_camel = name_camel,
290+
impl {pass_type}{pass_lifetimes} for {name_camel} {{}}
291+
"#
309292
)
310293
});
311294

312295
result
313296
}
314297

315298
fn get_lint_declaration(name_upper: &str, category: &str) -> String {
316-
format!(
317-
indoc! {r#"
299+
formatdoc!(
300+
r#"
318301
declare_clippy_lint! {{
319302
/// ### What it does
320303
///
@@ -328,15 +311,13 @@ fn get_lint_declaration(name_upper: &str, category: &str) -> String {
328311
/// ```rust
329312
/// // example code which does not raise clippy warning
330313
/// ```
331-
#[clippy::version = "{version}"]
314+
#[clippy::version = "{}"]
332315
pub {name_upper},
333316
{category},
334317
"default lint description"
335318
}}
336-
"#},
337-
version = get_stabilization_version(),
338-
name_upper = name_upper,
339-
category = category,
319+
"#,
320+
get_stabilization_version(),
340321
)
341322
}
342323

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

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

412393
write_file(lint_file_path.as_path(), lint_file_contents)?;
413-
println!("Generated lint file: `clippy_lints/src/{}/{}.rs`", ty, lint.name);
394+
println!("Generated lint file: `clippy_lints/src/{ty}/{}.rs`", lint.name);
414395
println!(
415-
"Be sure to add a call to `{}::check` in `clippy_lints/src/{}/mod.rs`!",
416-
lint.name, ty
396+
"Be sure to add a call to `{}::check` in `clippy_lints/src/{ty}/mod.rs`!",
397+
lint.name
417398
);
418399

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

clippy_dev/src/serve.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ use std::time::{Duration, SystemTime};
1010
/// Panics if the python commands could not be spawned
1111
pub fn run(port: u16, lint: Option<&String>) -> ! {
1212
let mut url = Some(match lint {
13-
None => format!("http://localhost:{}", port),
14-
Some(lint) => format!("http://localhost:{}/#{}", port, lint),
13+
None => format!("http://localhost:{port}"),
14+
Some(lint) => format!("http://localhost:{port}/#{lint}"),
1515
});
1616

1717
loop {

clippy_dev/src/setup/git_hook.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,7 @@ pub fn install_hook(force_override: bool) {
3030
println!("info: the hook can be removed with `cargo dev remove git-hook`");
3131
println!("git hook successfully installed");
3232
},
33-
Err(err) => eprintln!(
34-
"error: unable to copy `{}` to `{}` ({})",
35-
HOOK_SOURCE_FILE, HOOK_TARGET_FILE, err
36-
),
33+
Err(err) => eprintln!("error: unable to copy `{HOOK_SOURCE_FILE}` to `{HOOK_TARGET_FILE}` ({err})"),
3734
}
3835
}
3936

@@ -77,7 +74,7 @@ pub fn remove_hook() {
7774

7875
fn delete_git_hook_file(path: &Path) -> bool {
7976
if let Err(err) = fs::remove_file(path) {
80-
eprintln!("error: unable to delete existing pre-commit git hook ({})", err);
77+
eprintln!("error: unable to delete existing pre-commit git hook ({err})");
8178
false
8279
} else {
8380
true

0 commit comments

Comments
 (0)