Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 3 more run-rustfix headers #4431

Merged
merged 3 commits into from Aug 26, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
102 changes: 102 additions & 0 deletions tests/ui/or_fun_call.fixed
@@ -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() {}
3 changes: 3 additions & 0 deletions tests/ui/or_fun_call.rs
@@ -1,4 +1,7 @@
// run-rustfix

#![warn(clippy::or_fun_call)]
#![allow(dead_code)]

use std::collections::BTreeMap;
use std::collections::HashMap;
Expand Down
28 changes: 14 additions & 14 deletions tests/ui/or_fun_call.stderr
@@ -1,85 +1,85 @@
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());
| ^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(make)`
|
= note: `-D clippy::or-fun-call` implied by `-D warnings`

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());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `with_new.unwrap_or_default()`

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));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|| Vec::with_capacity(12))`

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());
| ^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|_| make())`

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));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|_| Vec::with_capacity(12))`

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());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `with_default_trait.unwrap_or_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());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `with_default_type.unwrap_or_default()`

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![]);
| ^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|| vec![])`

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());
| ^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(Foo::new)`

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());
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `or_insert_with(String::new)`

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());
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `or_insert_with(String::new)`

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());
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|| "".to_owned())`

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));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `ok_or_else(|| format!("{} world.", hello))`

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()));
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `or_else(|| Some("b".to_string()))`
Expand Down
37 changes: 37 additions & 0 deletions tests/ui/range_plus_minus_one.fixed
@@ -0,0 +1,37 @@
// run-rustfix

#![allow(unused_parens)]

fn f() -> usize {
42
}

#[warn(clippy::range_plus_one)]
fn main() {
for _ in 0..2 {}
for _ in 0..=2 {}

for _ in 0..=3 {}
for _ in 0..=3 + 1 {}

for _ in 0..=5 {}
for _ in 0..=1 + 5 {}

for _ in 1..=1 {}
for _ in 1..=1 + 1 {}

for _ in 0..13 + 13 {}
for _ in 0..=13 - 7 {}

for _ in 0..=f() {}
for _ in 0..=(1 + f()) {}

let _ = ..11 - 1;
let _ = ..11;
let _ = ..11;
let _ = (1..=11);
let _ = ((f() + 1)..=f());

let mut vec: Vec<()> = std::vec::Vec::new();
vec.drain(..);
}
4 changes: 4 additions & 0 deletions tests/ui/range_plus_minus_one.rs
@@ -1,3 +1,7 @@
// run-rustfix

#![allow(unused_parens)]

fn f() -> usize {
42
}
Expand Down
16 changes: 8 additions & 8 deletions tests/ui/range_plus_minus_one.stderr
@@ -1,51 +1,51 @@
error: an inclusive range would be more readable
--> $DIR/range_plus_minus_one.rs:10:14
--> $DIR/range_plus_minus_one.rs:14:14
|
LL | for _ in 0..3 + 1 {}
| ^^^^^^^^ help: use: `0..=3`
|
= note: `-D clippy::range-plus-one` implied by `-D warnings`

error: an inclusive range would be more readable
--> $DIR/range_plus_minus_one.rs:13:14
--> $DIR/range_plus_minus_one.rs:17:14
|
LL | for _ in 0..1 + 5 {}
| ^^^^^^^^ help: use: `0..=5`

error: an inclusive range would be more readable
--> $DIR/range_plus_minus_one.rs:16:14
--> $DIR/range_plus_minus_one.rs:20:14
|
LL | for _ in 1..1 + 1 {}
| ^^^^^^^^ help: use: `1..=1`

error: an inclusive range would be more readable
--> $DIR/range_plus_minus_one.rs:22:14
--> $DIR/range_plus_minus_one.rs:26:14
|
LL | for _ in 0..(1 + f()) {}
| ^^^^^^^^^^^^ help: use: `0..=f()`

error: an exclusive range would be more readable
--> $DIR/range_plus_minus_one.rs:26:13
--> $DIR/range_plus_minus_one.rs:30:13
|
LL | let _ = ..=11 - 1;
| ^^^^^^^^^ help: use: `..11`
|
= note: `-D clippy::range-minus-one` implied by `-D warnings`

error: an exclusive range would be more readable
--> $DIR/range_plus_minus_one.rs:27:13
--> $DIR/range_plus_minus_one.rs:31:13
|
LL | let _ = ..=(11 - 1);
| ^^^^^^^^^^^ help: use: `..11`

error: an inclusive range would be more readable
--> $DIR/range_plus_minus_one.rs:28:13
--> $DIR/range_plus_minus_one.rs:32:13
|
LL | let _ = (1..11 + 1);
| ^^^^^^^^^^^ help: use: `(1..=11)`

error: an inclusive range would be more readable
--> $DIR/range_plus_minus_one.rs:29:13
--> $DIR/range_plus_minus_one.rs:33:13
|
LL | let _ = (f() + 1)..(f() + 1);
| ^^^^^^^^^^^^^^^^^^^^ help: use: `((f() + 1)..=f())`
Expand Down
18 changes: 18 additions & 0 deletions tests/ui/short_circuit_statement.fixed
@@ -0,0 +1,18 @@
// run-rustfix

#![warn(clippy::short_circuit_statement)]
#![allow(clippy::nonminimal_bool)]

fn main() {
if f() { g(); }
if !f() { g(); }
if !(1 == 2) { g(); }
}

fn f() -> bool {
true
}

fn g() -> bool {
false
}
3 changes: 3 additions & 0 deletions tests/ui/short_circuit_statement.rs
@@ -1,4 +1,7 @@
// run-rustfix

#![warn(clippy::short_circuit_statement)]
#![allow(clippy::nonminimal_bool)]

fn main() {
f() && g();
Expand Down
6 changes: 3 additions & 3 deletions tests/ui/short_circuit_statement.stderr
@@ -1,19 +1,19 @@
error: boolean short circuit operator in statement may be clearer using an explicit test
--> $DIR/short_circuit_statement.rs:4:5
--> $DIR/short_circuit_statement.rs:7:5
|
LL | f() && g();
| ^^^^^^^^^^^ help: replace it with: `if f() { g(); }`
|
= note: `-D clippy::short-circuit-statement` implied by `-D warnings`

error: boolean short circuit operator in statement may be clearer using an explicit test
--> $DIR/short_circuit_statement.rs:5:5
--> $DIR/short_circuit_statement.rs:8:5
|
LL | f() || g();
| ^^^^^^^^^^^ help: replace it with: `if !f() { g(); }`

error: boolean short circuit operator in statement may be clearer using an explicit test
--> $DIR/short_circuit_statement.rs:6:5
--> $DIR/short_circuit_statement.rs:9:5
|
LL | 1 == 2 || g();
| ^^^^^^^^^^^^^^ help: replace it with: `if !(1 == 2) { g(); }`
Expand Down