diff options
| author | Philipp Krones <hello@philkrones.com> | 2022-09-28 14:27:32 +0200 |
|---|---|---|
| committer | Philipp Krones <hello@philkrones.com> | 2022-09-28 14:27:32 +0200 |
| commit | bbcde666853d11f6f5f3bdc660f018cf7fc8cd71 (patch) | |
| tree | 28df6eeea41c8e772bb65a172962f74282db1de1 /src | |
| parent | e5ce6d18df883f593e72f7958bebdc3ebcdbe85e (diff) | |
| parent | 0f6932a1f7623663e50922225ea304340949c051 (diff) | |
| download | rust-bbcde666853d11f6f5f3bdc660f018cf7fc8cd71.tar.gz rust-bbcde666853d11f6f5f3bdc660f018cf7fc8cd71.zip | |
Merge remote-tracking branch 'upstream/master' into rustup
Diffstat (limited to 'src')
| -rw-r--r-- | src/docs.rs | 2 | ||||
| -rw-r--r-- | src/docs/arithmetic_side_effects.txt | 4 | ||||
| -rw-r--r-- | src/docs/box_default.txt | 23 | ||||
| -rw-r--r-- | src/docs/uninlined_format_args.txt | 36 | ||||
| -rw-r--r-- | src/driver.rs | 6 | ||||
| -rw-r--r-- | src/main.rs | 6 |
6 files changed, 69 insertions, 8 deletions
diff --git a/src/docs.rs b/src/docs.rs index 6c89b4dde37..166be0618ff 100644 --- a/src/docs.rs +++ b/src/docs.rs @@ -48,6 +48,7 @@ docs! { "borrow_interior_mutable_const", "borrowed_box", "box_collection", + "box_default", "boxed_local", "branches_sharing_code", "builtin_type_shadow", @@ -521,6 +522,7 @@ docs! { "unimplemented", "uninit_assumed_init", "uninit_vec", + "uninlined_format_args", "unit_arg", "unit_cmp", "unit_hash", diff --git a/src/docs/arithmetic_side_effects.txt b/src/docs/arithmetic_side_effects.txt index 6c7d51a4989..b8c0872522e 100644 --- a/src/docs/arithmetic_side_effects.txt +++ b/src/docs/arithmetic_side_effects.txt @@ -5,7 +5,7 @@ Operators like `+`, `-`, `*` or `<<` are usually capable of overflowing accordin Reference](https://doc.rust-lang.org/reference/expressions/operator-expr.html#overflow), or can panic (`/`, `%`). -Known safe built-in types like `Wrapping` or `Saturing`, floats, operations in constant +Known safe built-in types like `Wrapping` or `Saturating`, floats, operations in constant environments, allowed types and non-constant operations that won't overflow are ignored. ### Why is this bad? @@ -30,4 +30,4 @@ let _n = Decimal::MAX + Decimal::MAX; ``` ### Allowed types -Custom allowed types can be specified through the "arithmetic-side-effects-allowed" filter. \ No newline at end of file +Custom allowed types can be specified through the "arithmetic-side-effects-allowed" filter. diff --git a/src/docs/box_default.txt b/src/docs/box_default.txt new file mode 100644 index 00000000000..ffac894d0c5 --- /dev/null +++ b/src/docs/box_default.txt @@ -0,0 +1,23 @@ +### What it does +checks for `Box::new(T::default())`, which is better written as +`Box::<T>::default()`. + +### Why is this bad? +First, it's more complex, involving two calls instead of one. +Second, `Box::default()` can be faster +[in certain cases](https://nnethercote.github.io/perf-book/standard-library-types.html#box). + +### Known problems +The lint may miss some cases (e.g. Box::new(String::from(""))). +On the other hand, it will trigger on cases where the `default` +code comes from a macro that does something different based on +e.g. target operating system. + +### Example +``` +let x: Box<String> = Box::new(Default::default()); +``` +Use instead: +``` +let x: Box<String> = Box::default(); +``` \ No newline at end of file diff --git a/src/docs/uninlined_format_args.txt b/src/docs/uninlined_format_args.txt new file mode 100644 index 00000000000..3d2966c84db --- /dev/null +++ b/src/docs/uninlined_format_args.txt @@ -0,0 +1,36 @@ +### What it does +Detect when a variable is not inlined in a format string, +and suggests to inline it. + +### Why is this bad? +Non-inlined code is slightly more difficult to read and understand, +as it requires arguments to be matched against the format string. +The inlined syntax, where allowed, is simpler. + +### Example +``` +format!("{}", var); +format!("{v:?}", v = var); +format!("{0} {0}", var); +format!("{0:1$}", var, width); +format!("{:.*}", prec, var); +``` +Use instead: +``` +format!("{var}"); +format!("{var:?}"); +format!("{var} {var}"); +format!("{var:width$}"); +format!("{var:.prec$}"); +``` + +### Known Problems + +There may be a false positive if the format string is expanded from certain proc macros: + +``` +println!(indoc!("{}"), var); +``` + +If a format string contains a numbered argument that cannot be inlined +nothing will be suggested, e.g. `println!("{0}={1}", var, 1+2)`. \ No newline at end of file diff --git a/src/driver.rs b/src/driver.rs index 235eae5af1e..b12208ac62a 100644 --- a/src/driver.rs +++ b/src/driver.rs @@ -193,8 +193,8 @@ fn report_clippy_ice(info: &panic::PanicInfo<'_>, bug_report_url: &str) { let xs: Vec<Cow<'static, str>> = vec![ "the compiler unexpectedly panicked. this is a bug.".into(), - format!("we would appreciate a bug report: {}", bug_report_url).into(), - format!("Clippy version: {}", version_info).into(), + format!("we would appreciate a bug report: {bug_report_url}").into(), + format!("Clippy version: {version_info}").into(), ]; for note in &xs { @@ -290,7 +290,7 @@ pub fn main() { if orig_args.iter().any(|a| a == "--version" || a == "-V") { let version_info = rustc_tools_util::get_version_info!(); - println!("{}", version_info); + println!("{version_info}"); exit(0); } diff --git a/src/main.rs b/src/main.rs index 4a32e0e54a8..fce3cdfc462 100644 --- a/src/main.rs +++ b/src/main.rs @@ -37,12 +37,12 @@ You can use tool lints to allow or deny lints from your code, eg.: "#; fn show_help() { - println!("{}", CARGO_CLIPPY_HELP); + println!("{CARGO_CLIPPY_HELP}"); } fn show_version() { let version_info = rustc_tools_util::get_version_info!(); - println!("{}", version_info); + println!("{version_info}"); } pub fn main() { @@ -133,7 +133,7 @@ impl ClippyCmd { let clippy_args: String = self .clippy_args .iter() - .map(|arg| format!("{}__CLIPPY_HACKERY__", arg)) + .map(|arg| format!("{arg}__CLIPPY_HACKERY__")) .collect(); // Currently, `CLIPPY_TERMINAL_WIDTH` is used only to format "unknown field" error messages. |
