about summary refs log tree commit diff
path: root/src/docs
diff options
context:
space:
mode:
authorPhilipp Krones <hello@philkrones.com>2022-10-06 09:44:38 +0200
committerPhilipp Krones <hello@philkrones.com>2022-10-06 09:44:38 +0200
commitd75b25faabdcf0a22fe37928917c4ab1761fa265 (patch)
treee8c46d2dae51a0a61a6d28de138ca9add8276d8d /src/docs
parentda16cc1da9814710e637ff242b71768a4d3724b7 (diff)
downloadrust-d75b25faabdcf0a22fe37928917c4ab1761fa265.tar.gz
rust-d75b25faabdcf0a22fe37928917c4ab1761fa265.zip
Merge commit 'ac0e10aa68325235069a842f47499852b2dee79e' into clippyup
Diffstat (limited to 'src/docs')
-rw-r--r--src/docs/arithmetic_side_effects.txt2
-rw-r--r--src/docs/box_default.txt23
-rw-r--r--src/docs/disallowed_macros.txt36
-rw-r--r--src/docs/implicit_saturating_add.txt20
-rw-r--r--src/docs/manual_clamp.txt46
-rw-r--r--src/docs/needless_borrowed_reference.txt18
-rw-r--r--src/docs/similar_names.txt4
-rw-r--r--src/docs/uninlined_format_args.txt36
8 files changed, 171 insertions, 14 deletions
diff --git a/src/docs/arithmetic_side_effects.txt b/src/docs/arithmetic_side_effects.txt
index 6c7d51a4989..4ae8bce88ad 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?
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/disallowed_macros.txt b/src/docs/disallowed_macros.txt
new file mode 100644
index 00000000000..96fa15afabf
--- /dev/null
+++ b/src/docs/disallowed_macros.txt
@@ -0,0 +1,36 @@
+### What it does
+Denies the configured macros in clippy.toml
+
+Note: Even though this lint is warn-by-default, it will only trigger if
+macros are defined in the clippy.toml file.
+
+### Why is this bad?
+Some macros are undesirable in certain contexts, and it's beneficial to
+lint for them as needed.
+
+### Example
+An example clippy.toml configuration:
+```
+disallowed-macros = [
+    # Can use a string as the path of the disallowed macro.
+    "std::print",
+    # Can also use an inline table with a `path` key.
+    { path = "std::println" },
+    # When using an inline table, can add a `reason` for why the macro
+    # is disallowed.
+    { path = "serde::Serialize", reason = "no serializing" },
+]
+```
+```
+use serde::Serialize;
+
+// Example code where clippy issues a warning
+println!("warns");
+
+// The diagnostic will contain the message "no serializing"
+#[derive(Serialize)]
+struct Data {
+    name: String,
+    value: usize,
+}
+```
\ No newline at end of file
diff --git a/src/docs/implicit_saturating_add.txt b/src/docs/implicit_saturating_add.txt
new file mode 100644
index 00000000000..5883a5363e2
--- /dev/null
+++ b/src/docs/implicit_saturating_add.txt
@@ -0,0 +1,20 @@
+### What it does
+Checks for implicit saturating addition.
+
+### Why is this bad?
+The built-in function is more readable and may be faster.
+
+### Example
+```
+let mut u:u32 = 7000;
+
+if u != u32::MAX {
+    u += 1;
+}
+```
+Use instead:
+```
+let mut u:u32 = 7000;
+
+u = u.saturating_add(1);
+```
\ No newline at end of file
diff --git a/src/docs/manual_clamp.txt b/src/docs/manual_clamp.txt
new file mode 100644
index 00000000000..8993f6683ad
--- /dev/null
+++ b/src/docs/manual_clamp.txt
@@ -0,0 +1,46 @@
+### What it does
+Identifies good opportunities for a clamp function from std or core, and suggests using it.
+
+### Why is this bad?
+clamp is much shorter, easier to read, and doesn't use any control flow.
+
+### Known issue(s)
+If the clamped variable is NaN this suggestion will cause the code to propagate NaN
+rather than returning either `max` or `min`.
+
+`clamp` functions will panic if `max < min`, `max.is_nan()`, or `min.is_nan()`.
+Some may consider panicking in these situations to be desirable, but it also may
+introduce panicking where there wasn't any before.
+
+### Examples
+```
+if input > max {
+    max
+} else if input < min {
+    min
+} else {
+    input
+}
+```
+
+```
+input.max(min).min(max)
+```
+
+```
+match input {
+    x if x > max => max,
+    x if x < min => min,
+    x => x,
+}
+```
+
+```
+let mut x = input;
+if x < min { x = min; }
+if x > max { x = max; }
+```
+Use instead:
+```
+input.clamp(min, max)
+```
\ No newline at end of file
diff --git a/src/docs/needless_borrowed_reference.txt b/src/docs/needless_borrowed_reference.txt
index 55faa0cf571..152459ba1c9 100644
--- a/src/docs/needless_borrowed_reference.txt
+++ b/src/docs/needless_borrowed_reference.txt
@@ -1,30 +1,22 @@
 ### What it does
-Checks for bindings that destructure a reference and borrow the inner
+Checks for bindings that needlessly destructure a reference and borrow the inner
 value with `&ref`.
 
 ### Why is this bad?
 This pattern has no effect in almost all cases.
 
-### Known problems
-In some cases, `&ref` is needed to avoid a lifetime mismatch error.
-Example:
-```
-fn foo(a: &Option<String>, b: &Option<String>) {
-    match (a, b) {
-        (None, &ref c) | (&ref c, None) => (),
-        (&Some(ref c), _) => (),
-    };
-}
-```
-
 ### Example
 ```
 let mut v = Vec::<String>::new();
 v.iter_mut().filter(|&ref a| a.is_empty());
+
+if let &[ref first, ref second] = v.as_slice() {}
 ```
 
 Use instead:
 ```
 let mut v = Vec::<String>::new();
 v.iter_mut().filter(|a| a.is_empty());
+
+if let [first, second] = v.as_slice() {}
 ```
\ No newline at end of file
diff --git a/src/docs/similar_names.txt b/src/docs/similar_names.txt
index 13aca9c0bb7..f9eff21b679 100644
--- a/src/docs/similar_names.txt
+++ b/src/docs/similar_names.txt
@@ -1,6 +1,10 @@
 ### What it does
 Checks for names that are very similar and thus confusing.
 
+Note: this lint looks for similar names throughout each
+scope. To allow it, you need to allow it on the scope
+level, not on the name that is reported.
+
 ### Why is this bad?
 It's hard to distinguish between names that differ only
 by a single character.
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