2023-04-20 14:37:15 +00:00
|
|
|
//@run-rustfix
|
2019-08-22 05:41:00 +00:00
|
|
|
#![warn(clippy::or_fun_call)]
|
|
|
|
#![allow(dead_code)]
|
2023-06-06 20:56:57 +00:00
|
|
|
#![allow(
|
|
|
|
clippy::borrow_as_ptr,
|
|
|
|
clippy::uninlined_format_args,
|
|
|
|
clippy::unnecessary_wraps,
|
2023-05-31 17:47:10 +00:00
|
|
|
clippy::unnecessary_literal_unwrap,
|
2023-06-06 20:56:57 +00:00
|
|
|
clippy::useless_vec
|
|
|
|
)]
|
2019-08-22 05:41:00 +00:00
|
|
|
|
|
|
|
use std::collections::BTreeMap;
|
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::time::Duration;
|
|
|
|
|
|
|
|
/// Checks implementation of the `OR_FUN_CALL` lint.
|
|
|
|
fn or_fun_call() {
|
|
|
|
struct Foo;
|
|
|
|
|
|
|
|
impl Foo {
|
|
|
|
fn new() -> Foo {
|
|
|
|
Foo
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-12 09:16:25 +00:00
|
|
|
struct FakeDefault;
|
|
|
|
impl FakeDefault {
|
|
|
|
fn default() -> Self {
|
|
|
|
FakeDefault
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for FakeDefault {
|
|
|
|
fn default() -> Self {
|
|
|
|
FakeDefault
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-22 05:41:00 +00:00
|
|
|
enum Enum {
|
|
|
|
A(i32),
|
|
|
|
}
|
|
|
|
|
|
|
|
fn make<T>() -> T {
|
|
|
|
unimplemented!();
|
|
|
|
}
|
|
|
|
|
|
|
|
let with_enum = Some(Enum::A(1));
|
|
|
|
with_enum.unwrap_or(Enum::A(5));
|
|
|
|
|
|
|
|
let with_const_fn = Some(Duration::from_secs(1));
|
2021-12-06 11:33:31 +00:00
|
|
|
with_const_fn.unwrap_or(Duration::from_secs(5));
|
2019-08-22 05:41:00 +00:00
|
|
|
|
|
|
|
let with_constructor = Some(vec![1]);
|
|
|
|
with_constructor.unwrap_or_else(make);
|
|
|
|
|
|
|
|
let with_new = Some(vec![1]);
|
|
|
|
with_new.unwrap_or_default();
|
|
|
|
|
|
|
|
let with_const_args = Some(vec![1]);
|
|
|
|
with_const_args.unwrap_or_else(|| Vec::with_capacity(12));
|
|
|
|
|
|
|
|
let with_err: Result<_, ()> = Ok(vec![1]);
|
|
|
|
with_err.unwrap_or_else(|_| make());
|
|
|
|
|
|
|
|
let with_err_args: Result<_, ()> = Ok(vec![1]);
|
|
|
|
with_err_args.unwrap_or_else(|_| Vec::with_capacity(12));
|
|
|
|
|
|
|
|
let with_default_trait = Some(1);
|
|
|
|
with_default_trait.unwrap_or_default();
|
|
|
|
|
|
|
|
let with_default_type = Some(1);
|
|
|
|
with_default_type.unwrap_or_default();
|
|
|
|
|
2021-08-12 09:16:25 +00:00
|
|
|
let self_default = None::<FakeDefault>;
|
|
|
|
self_default.unwrap_or_else(<FakeDefault>::default);
|
|
|
|
|
|
|
|
let real_default = None::<FakeDefault>;
|
|
|
|
real_default.unwrap_or_default();
|
|
|
|
|
2019-08-22 05:41:00 +00:00
|
|
|
let with_vec = Some(vec![1]);
|
2020-04-01 18:14:05 +00:00
|
|
|
with_vec.unwrap_or_default();
|
2019-08-22 05:41:00 +00:00
|
|
|
|
|
|
|
let without_default = Some(Foo);
|
|
|
|
without_default.unwrap_or_else(Foo::new);
|
|
|
|
|
2020-10-09 10:45:29 +00:00
|
|
|
let mut map = HashMap::<u64, String>::new();
|
2022-08-16 20:26:03 +00:00
|
|
|
map.entry(42).or_default();
|
2020-10-09 10:45:29 +00:00
|
|
|
|
2021-03-12 14:30:50 +00:00
|
|
|
let mut map_vec = HashMap::<u64, Vec<i32>>::new();
|
2022-08-16 20:26:03 +00:00
|
|
|
map_vec.entry(42).or_default();
|
2021-03-12 14:30:50 +00:00
|
|
|
|
2020-10-09 10:45:29 +00:00
|
|
|
let mut btree = BTreeMap::<u64, String>::new();
|
2022-08-16 20:26:03 +00:00
|
|
|
btree.entry(42).or_default();
|
2020-10-09 10:45:29 +00:00
|
|
|
|
2021-03-12 14:30:50 +00:00
|
|
|
let mut btree_vec = BTreeMap::<u64, Vec<i32>>::new();
|
2022-08-16 20:26:03 +00:00
|
|
|
btree_vec.entry(42).or_default();
|
2021-03-12 14:30:50 +00:00
|
|
|
|
2022-08-05 23:59:50 +00:00
|
|
|
let stringy = Some(String::new());
|
|
|
|
let _ = stringy.unwrap_or_default();
|
2019-08-22 05:41:00 +00:00
|
|
|
|
|
|
|
let opt = Some(1);
|
|
|
|
let hello = "Hello";
|
2019-11-25 12:56:24 +00:00
|
|
|
let _ = opt.ok_or(format!("{} world.", hello));
|
2020-11-23 12:51:04 +00:00
|
|
|
|
|
|
|
// index
|
|
|
|
let map = HashMap::<u64, u64>::new();
|
|
|
|
let _ = Some(1).unwrap_or_else(|| map[&1]);
|
|
|
|
let map = BTreeMap::<u64, u64>::new();
|
|
|
|
let _ = Some(1).unwrap_or_else(|| map[&1]);
|
|
|
|
// don't lint index vec
|
|
|
|
let vec = vec![1];
|
|
|
|
let _ = Some(1).unwrap_or(vec[1]);
|
2019-08-22 05:41:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct Foo(u8);
|
|
|
|
struct Bar(String, Duration);
|
|
|
|
#[rustfmt::skip]
|
|
|
|
fn test_or_with_ctors() {
|
|
|
|
let opt = Some(1);
|
|
|
|
let opt_opt = Some(Some(1));
|
|
|
|
// we also test for const promotion, this makes sure we don't hit that
|
|
|
|
let two = 2;
|
|
|
|
|
|
|
|
let _ = opt_opt.unwrap_or(Some(2));
|
|
|
|
let _ = opt_opt.unwrap_or(Some(two));
|
|
|
|
let _ = opt.ok_or(Some(2));
|
|
|
|
let _ = opt.ok_or(Some(two));
|
|
|
|
let _ = opt.ok_or(Foo(2));
|
|
|
|
let _ = opt.ok_or(Foo(two));
|
|
|
|
let _ = opt.or(Some(2));
|
|
|
|
let _ = opt.or(Some(two));
|
|
|
|
|
|
|
|
let _ = Some("a".to_string()).or_else(|| Some("b".to_string()));
|
|
|
|
|
|
|
|
let b = "b".to_string();
|
|
|
|
let _ = Some(Bar("a".to_string(), Duration::from_secs(1)))
|
2021-12-06 11:33:31 +00:00
|
|
|
.or(Some(Bar(b, Duration::from_secs(2))));
|
2020-05-28 13:45:24 +00:00
|
|
|
|
|
|
|
let vec = vec!["foo"];
|
|
|
|
let _ = opt.ok_or(vec.len());
|
|
|
|
|
|
|
|
let array = ["foo"];
|
|
|
|
let _ = opt.ok_or(array.len());
|
|
|
|
|
|
|
|
let slice = &["foo"][..];
|
|
|
|
let _ = opt.ok_or(slice.len());
|
2021-03-25 18:29:11 +00:00
|
|
|
|
|
|
|
let string = "foo";
|
|
|
|
let _ = opt.ok_or(string.len());
|
2019-08-22 05:41:00 +00:00
|
|
|
}
|
|
|
|
|
2019-09-08 09:03:45 +00:00
|
|
|
// Issue 4514 - early return
|
|
|
|
fn f() -> Option<()> {
|
|
|
|
let a = Some(1);
|
|
|
|
let b = 1i32;
|
|
|
|
|
|
|
|
let _ = a.unwrap_or(b.checked_mul(3)?.min(240));
|
|
|
|
|
|
|
|
Some(())
|
|
|
|
}
|
|
|
|
|
2021-03-25 18:29:11 +00:00
|
|
|
mod issue6675 {
|
2021-12-06 11:33:31 +00:00
|
|
|
unsafe fn ptr_to_ref<'a, T>(p: *const T) -> &'a T {
|
|
|
|
#[allow(unused)]
|
|
|
|
let x = vec![0; 1000]; // future-proofing, make this function expensive.
|
|
|
|
&*p
|
|
|
|
}
|
|
|
|
|
2021-03-25 18:29:11 +00:00
|
|
|
unsafe fn foo() {
|
2021-12-06 11:33:31 +00:00
|
|
|
let s = "test".to_owned();
|
|
|
|
let s = &s as *const _;
|
|
|
|
None.unwrap_or_else(|| ptr_to_ref(s));
|
2021-03-25 18:29:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn bar() {
|
2021-12-06 11:33:31 +00:00
|
|
|
let s = "test".to_owned();
|
|
|
|
let s = &s as *const _;
|
|
|
|
None.unwrap_or_else(|| unsafe { ptr_to_ref(s) });
|
2021-03-25 18:29:11 +00:00
|
|
|
#[rustfmt::skip]
|
2021-12-06 11:33:31 +00:00
|
|
|
None.unwrap_or_else(|| unsafe { ptr_to_ref(s) });
|
2021-03-25 18:29:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-17 12:29:07 +00:00
|
|
|
mod issue8239 {
|
|
|
|
fn more_than_max_suggestion_highest_lines_0() {
|
|
|
|
let frames = Vec::new();
|
|
|
|
frames
|
|
|
|
.iter()
|
|
|
|
.map(|f: &String| f.to_lowercase())
|
|
|
|
.reduce(|mut acc, f| {
|
|
|
|
acc.push_str(&f);
|
|
|
|
acc
|
2022-07-10 10:11:19 +00:00
|
|
|
})
|
|
|
|
.unwrap_or_default();
|
2022-01-17 12:29:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn more_to_max_suggestion_highest_lines_1() {
|
|
|
|
let frames = Vec::new();
|
|
|
|
let iter = frames.iter();
|
|
|
|
iter.map(|f: &String| f.to_lowercase())
|
|
|
|
.reduce(|mut acc, f| {
|
|
|
|
let _ = "";
|
|
|
|
let _ = "";
|
|
|
|
acc.push_str(&f);
|
|
|
|
acc
|
2022-07-10 10:11:19 +00:00
|
|
|
})
|
|
|
|
.unwrap_or_default();
|
2022-01-17 12:29:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn equal_to_max_suggestion_highest_lines() {
|
|
|
|
let frames = Vec::new();
|
|
|
|
let iter = frames.iter();
|
|
|
|
iter.map(|f: &String| f.to_lowercase())
|
|
|
|
.reduce(|mut acc, f| {
|
|
|
|
let _ = "";
|
|
|
|
acc.push_str(&f);
|
|
|
|
acc
|
2022-07-10 10:11:19 +00:00
|
|
|
})
|
|
|
|
.unwrap_or_default();
|
2022-01-17 12:29:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn less_than_max_suggestion_highest_lines() {
|
|
|
|
let frames = Vec::new();
|
|
|
|
let iter = frames.iter();
|
|
|
|
let map = iter.map(|f: &String| f.to_lowercase());
|
|
|
|
map.reduce(|mut acc, f| {
|
|
|
|
acc.push_str(&f);
|
|
|
|
acc
|
2022-07-10 10:11:19 +00:00
|
|
|
})
|
|
|
|
.unwrap_or_default();
|
2022-01-17 12:29:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-08 15:23:05 +00:00
|
|
|
mod issue9608 {
|
|
|
|
fn sig_drop() {
|
|
|
|
enum X {
|
|
|
|
X(std::fs::File),
|
|
|
|
Y(u32),
|
|
|
|
}
|
|
|
|
|
|
|
|
let _ = None.unwrap_or(X::Y(0));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-22 07:36:36 +00:00
|
|
|
mod issue8993 {
|
|
|
|
fn g() -> i32 {
|
|
|
|
3
|
|
|
|
}
|
|
|
|
|
|
|
|
fn f(n: i32) -> i32 {
|
|
|
|
n
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_map_or() {
|
|
|
|
let _ = Some(4).map_or_else(g, |v| v);
|
|
|
|
let _ = Some(4).map_or_else(g, f);
|
|
|
|
let _ = Some(4).map_or(0, f);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-22 05:41:00 +00:00
|
|
|
fn main() {}
|