Skip to content

Commit 493b788

Browse files
committed
Auto merge of #9829 - hrxi:pr_or_fun_call, r=llogiq
Make it clear that `or_fun_call` can be a false-positive Also move it to nursery so that the false-positives can be dealt with. CC #8574 changelog: [`or_fun_call`]: Mention false-positives, move to nursery.
2 parents 6ba3a00 + 243661b commit 493b788

File tree

2 files changed

+11
-13
lines changed

2 files changed

+11
-13
lines changed

clippy_lints/src/methods/mod.rs

+10-12
Original file line numberDiff line numberDiff line change
@@ -831,32 +831,30 @@ declare_clippy_lint! {
831831
/// etc. instead.
832832
///
833833
/// ### Why is this bad?
834-
/// The function will always be called and potentially
835-
/// allocate an object acting as the default.
834+
/// The function will always be called. This is only bad if it allocates or
835+
/// does some non-trivial amount of work.
836836
///
837837
/// ### Known problems
838-
/// If the function has side-effects, not calling it will
839-
/// change the semantic of the program, but you shouldn't rely on that anyway.
838+
/// If the function has side-effects, not calling it will change the
839+
/// semantic of the program, but you shouldn't rely on that.
840+
///
841+
/// The lint also cannot figure out whether the function you call is
842+
/// actually expensive to call or not.
840843
///
841844
/// ### Example
842845
/// ```rust
843846
/// # let foo = Some(String::new());
844-
/// foo.unwrap_or(String::new());
847+
/// foo.unwrap_or(String::from("empty"));
845848
/// ```
846849
///
847850
/// Use instead:
848851
/// ```rust
849852
/// # let foo = Some(String::new());
850-
/// foo.unwrap_or_else(String::new);
851-
///
852-
/// // or
853-
///
854-
/// # let foo = Some(String::new());
855-
/// foo.unwrap_or_default();
853+
/// foo.unwrap_or_else(|| String::from("empty"));
856854
/// ```
857855
#[clippy::version = "pre 1.29.0"]
858856
pub OR_FUN_CALL,
859-
perf,
857+
nursery,
860858
"using any `*or` method with a function call, which suggests `*or_else`"
861859
}
862860

tests/ui/unwrap_or.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![warn(clippy::all)]
1+
#![warn(clippy::all, clippy::or_fun_call)]
22

33
fn main() {
44
let s = Some(String::from("test string")).unwrap_or("Fail".to_string()).len();

0 commit comments

Comments
 (0)