Add run-rustfix for or_fun_call tests

This commit is contained in:
Philipp Hansch 2019-08-22 07:41:00 +02:00
parent 9bda1e2264
commit 18fb1d9d2f
No known key found for this signature in database
GPG Key ID: 82AA61CAA11397E6
3 changed files with 119 additions and 14 deletions

102
tests/ui/or_fun_call.fixed Normal file
View File

@ -0,0 +1,102 @@
// run-rustfix
#![warn(clippy::or_fun_call)]
#![allow(dead_code)]
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
}
}
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));
with_const_fn.unwrap_or(Duration::from_secs(5));
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();
let with_vec = Some(vec![1]);
with_vec.unwrap_or_else(|| vec![]);
// FIXME #944: ~|SUGGESTION with_vec.unwrap_or_else(|| vec![]);
let without_default = Some(Foo);
without_default.unwrap_or_else(Foo::new);
let mut map = HashMap::<u64, String>::new();
map.entry(42).or_insert_with(String::new);
let mut btree = BTreeMap::<u64, String>::new();
btree.entry(42).or_insert_with(String::new);
let stringy = Some(String::from(""));
let _ = stringy.unwrap_or_else(|| "".to_owned());
let opt = Some(1);
let hello = "Hello";
let _ = opt.ok_or_else(|| format!("{} world.", hello));
}
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)))
.or(Some(Bar(b, Duration::from_secs(2))));
}
fn main() {}

View File

@ -1,4 +1,7 @@
// run-rustfix
#![warn(clippy::or_fun_call)] #![warn(clippy::or_fun_call)]
#![allow(dead_code)]
use std::collections::BTreeMap; use std::collections::BTreeMap;
use std::collections::HashMap; use std::collections::HashMap;

View File

@ -1,5 +1,5 @@
error: use of `unwrap_or` followed by a function call error: use of `unwrap_or` followed by a function call
--> $DIR/or_fun_call.rs:32:22 --> $DIR/or_fun_call.rs:35:22
| |
LL | with_constructor.unwrap_or(make()); LL | with_constructor.unwrap_or(make());
| ^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(make)` | ^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(make)`
@ -7,79 +7,79 @@ LL | with_constructor.unwrap_or(make());
= note: `-D clippy::or-fun-call` implied by `-D warnings` = note: `-D clippy::or-fun-call` implied by `-D warnings`
error: use of `unwrap_or` followed by a call to `new` error: use of `unwrap_or` followed by a call to `new`
--> $DIR/or_fun_call.rs:35:5 --> $DIR/or_fun_call.rs:38:5
| |
LL | with_new.unwrap_or(Vec::new()); LL | with_new.unwrap_or(Vec::new());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `with_new.unwrap_or_default()` | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `with_new.unwrap_or_default()`
error: use of `unwrap_or` followed by a function call error: use of `unwrap_or` followed by a function call
--> $DIR/or_fun_call.rs:38:21 --> $DIR/or_fun_call.rs:41:21
| |
LL | with_const_args.unwrap_or(Vec::with_capacity(12)); LL | with_const_args.unwrap_or(Vec::with_capacity(12));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|| Vec::with_capacity(12))` | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|| Vec::with_capacity(12))`
error: use of `unwrap_or` followed by a function call error: use of `unwrap_or` followed by a function call
--> $DIR/or_fun_call.rs:41:14 --> $DIR/or_fun_call.rs:44:14
| |
LL | with_err.unwrap_or(make()); LL | with_err.unwrap_or(make());
| ^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|_| make())` | ^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|_| make())`
error: use of `unwrap_or` followed by a function call error: use of `unwrap_or` followed by a function call
--> $DIR/or_fun_call.rs:44:19 --> $DIR/or_fun_call.rs:47:19
| |
LL | with_err_args.unwrap_or(Vec::with_capacity(12)); LL | with_err_args.unwrap_or(Vec::with_capacity(12));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|_| Vec::with_capacity(12))` | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|_| Vec::with_capacity(12))`
error: use of `unwrap_or` followed by a call to `default` error: use of `unwrap_or` followed by a call to `default`
--> $DIR/or_fun_call.rs:47:5 --> $DIR/or_fun_call.rs:50:5
| |
LL | with_default_trait.unwrap_or(Default::default()); LL | with_default_trait.unwrap_or(Default::default());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `with_default_trait.unwrap_or_default()` | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `with_default_trait.unwrap_or_default()`
error: use of `unwrap_or` followed by a call to `default` error: use of `unwrap_or` followed by a call to `default`
--> $DIR/or_fun_call.rs:50:5 --> $DIR/or_fun_call.rs:53:5
| |
LL | with_default_type.unwrap_or(u64::default()); LL | with_default_type.unwrap_or(u64::default());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `with_default_type.unwrap_or_default()` | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `with_default_type.unwrap_or_default()`
error: use of `unwrap_or` followed by a function call error: use of `unwrap_or` followed by a function call
--> $DIR/or_fun_call.rs:53:14 --> $DIR/or_fun_call.rs:56:14
| |
LL | with_vec.unwrap_or(vec![]); LL | with_vec.unwrap_or(vec![]);
| ^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|| vec![])` | ^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|| vec![])`
error: use of `unwrap_or` followed by a function call error: use of `unwrap_or` followed by a function call
--> $DIR/or_fun_call.rs:58:21 --> $DIR/or_fun_call.rs:61:21
| |
LL | without_default.unwrap_or(Foo::new()); LL | without_default.unwrap_or(Foo::new());
| ^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(Foo::new)` | ^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(Foo::new)`
error: use of `or_insert` followed by a function call error: use of `or_insert` followed by a function call
--> $DIR/or_fun_call.rs:61:19 --> $DIR/or_fun_call.rs:64:19
| |
LL | map.entry(42).or_insert(String::new()); LL | map.entry(42).or_insert(String::new());
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `or_insert_with(String::new)` | ^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `or_insert_with(String::new)`
error: use of `or_insert` followed by a function call error: use of `or_insert` followed by a function call
--> $DIR/or_fun_call.rs:64:21 --> $DIR/or_fun_call.rs:67:21
| |
LL | btree.entry(42).or_insert(String::new()); LL | btree.entry(42).or_insert(String::new());
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `or_insert_with(String::new)` | ^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `or_insert_with(String::new)`
error: use of `unwrap_or` followed by a function call error: use of `unwrap_or` followed by a function call
--> $DIR/or_fun_call.rs:67:21 --> $DIR/or_fun_call.rs:70:21
| |
LL | let _ = stringy.unwrap_or("".to_owned()); LL | let _ = stringy.unwrap_or("".to_owned());
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|| "".to_owned())` | ^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|| "".to_owned())`
error: use of `ok_or` followed by a function call error: use of `ok_or` followed by a function call
--> $DIR/or_fun_call.rs:71:17 --> $DIR/or_fun_call.rs:74:17
| |
LL | let _ = opt.ok_or(format!("{} world.", hello)); LL | let _ = opt.ok_or(format!("{} world.", hello));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `ok_or_else(|| format!("{} world.", hello))` | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `ok_or_else(|| format!("{} world.", hello))`
error: use of `or` followed by a function call error: use of `or` followed by a function call
--> $DIR/or_fun_call.rs:92:35 --> $DIR/or_fun_call.rs:95:35
| |
LL | let _ = Some("a".to_string()).or(Some("b".to_string())); LL | let _ = Some("a".to_string()).or(Some("b".to_string()));
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `or_else(|| Some("b".to_string()))` | ^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `or_else(|| Some("b".to_string()))`