Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 10 additions & 12 deletions clippy_lints/src/methods/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -832,32 +832,30 @@ declare_clippy_lint! {
/// etc. instead.
///
/// ### Why is this bad?
/// The function will always be called and potentially
/// allocate an object acting as the default.
/// The function will always be called. This is only bad if it allocates or
/// does some non-trivial amount of work.
///
/// ### Known problems
/// If the function has side-effects, not calling it will
/// change the semantic of the program, but you shouldn't rely on that anyway.
/// If the function has side-effects, not calling it will change the
/// semantic of the program, but you shouldn't rely on that.
///
/// The lint also cannot figure out whether the function you call is
/// actually expensive to call or not.
///
/// ### Example
/// ```rust
/// # let foo = Some(String::new());
/// foo.unwrap_or(String::new());
/// foo.unwrap_or(String::from("empty"));
/// ```
///
/// Use instead:
/// ```rust
/// # let foo = Some(String::new());
/// foo.unwrap_or_else(String::new);
///
/// // or
///
/// # let foo = Some(String::new());
/// foo.unwrap_or_default();
/// foo.unwrap_or_else(|| String::from("empty"));
/// ```
#[clippy::version = "pre 1.29.0"]
pub OR_FUN_CALL,
perf,
nursery,
"using any `*or` method with a function call, which suggests `*or_else`"
}

Expand Down
2 changes: 1 addition & 1 deletion tests/ui/unwrap_or.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![warn(clippy::all)]
#![warn(clippy::all, clippy::or_fun_call)]

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