Skip to content

Commit

Permalink
Added explicit return statement to shorten
Browse files Browse the repository at this point in the history
`is_string_or_str_slice`
  • Loading branch information
rsulli55 committed Oct 9, 2020
1 parent acc2ce4 commit a51e0ea
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 19 deletions.
22 changes: 12 additions & 10 deletions clippy_lints/src/find_is_some_on_strs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,16 @@ impl LateLintPass<'_> for FindIsSomeOnStrs {
}

fn is_string_or_str_slice(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
let is_string = is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(expr).peel_refs(), sym!(string_type));
// check if it is `str` or `&str`
let ty = cx.typeck_results().expr_ty(expr);
let is_str_slice = match ty.kind() {
ty::Str => true,
ty::Ref(_, inner, _) if *inner.kind() == ty::Str => true,
_ => false,
};

is_string || is_str_slice
// check if String
if is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(expr).peel_refs(), sym!(string_type)) {
true
} else {
// check if it is `str` or `&str`
let ty = cx.typeck_results().expr_ty(expr);
match ty.kind() {
ty::Str => true,
ty::Ref(_, inner, _) if *inner.kind() == ty::Str => true,
_ => false,
}
}
}
18 changes: 9 additions & 9 deletions tests/ui/find_is_some_on_strs.stderr
Original file line number Diff line number Diff line change
@@ -1,54 +1,54 @@
error: called `is_some()` after calling `find()` on a string. This is more succinctly expressed by calling `contains()`.
error: called `is_some()` after calling `find()` on a string. This is more succinctly expressed by calling `contains()`.
--> $DIR/find_is_some_on_strs.rs:15:27
|
LL | let _ = "hello world".find("world").is_some();
| ^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `contains("world")`
|
= note: `-D clippy::find-is-some-on-strs` implied by `-D warnings`

error: called `is_some()` after calling `find()` on a string. This is more succinctly expressed by calling `contains()`.
error: called `is_some()` after calling `find()` on a string. This is more succinctly expressed by calling `contains()`.
--> $DIR/find_is_some_on_strs.rs:16:27
|
LL | let _ = "hello world".find(&s2).is_some();
| ^^^^^^^^^^^^^^^^^^^ help: try this: `contains(&s2)`

error: called `is_some()` after calling `find()` on a string. This is more succinctly expressed by calling `contains()`.
error: called `is_some()` after calling `find()` on a string. This is more succinctly expressed by calling `contains()`.
--> $DIR/find_is_some_on_strs.rs:17:27
|
LL | let _ = "hello world".find(&s2[2..]).is_some();
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `contains(&s2[2..])`

error: called `is_some()` after calling `find()` on a string. This is more succinctly expressed by calling `contains()`.
error: called `is_some()` after calling `find()` on a string. This is more succinctly expressed by calling `contains()`.
--> $DIR/find_is_some_on_strs.rs:19:16
|
LL | let _ = s1.find("world").is_some();
| ^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `contains("world")`

error: called `is_some()` after calling `find()` on a string. This is more succinctly expressed by calling `contains()`.
error: called `is_some()` after calling `find()` on a string. This is more succinctly expressed by calling `contains()`.
--> $DIR/find_is_some_on_strs.rs:20:16
|
LL | let _ = s1.find(&s2).is_some();
| ^^^^^^^^^^^^^^^^^^^ help: try this: `contains(&s2)`

error: called `is_some()` after calling `find()` on a string. This is more succinctly expressed by calling `contains()`.
error: called `is_some()` after calling `find()` on a string. This is more succinctly expressed by calling `contains()`.
--> $DIR/find_is_some_on_strs.rs:21:16
|
LL | let _ = s1.find(&s2[2..]).is_some();
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `contains(&s2[2..])`

error: called `is_some()` after calling `find()` on a string. This is more succinctly expressed by calling `contains()`.
error: called `is_some()` after calling `find()` on a string. This is more succinctly expressed by calling `contains()`.
--> $DIR/find_is_some_on_strs.rs:23:21
|
LL | let _ = s1[2..].find("world").is_some();
| ^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `contains("world")`

error: called `is_some()` after calling `find()` on a string. This is more succinctly expressed by calling `contains()`.
error: called `is_some()` after calling `find()` on a string. This is more succinctly expressed by calling `contains()`.
--> $DIR/find_is_some_on_strs.rs:24:21
|
LL | let _ = s1[2..].find(&s2).is_some();
| ^^^^^^^^^^^^^^^^^^^ help: try this: `contains(&s2)`

error: called `is_some()` after calling `find()` on a string. This is more succinctly expressed by calling `contains()`.
error: called `is_some()` after calling `find()` on a string. This is more succinctly expressed by calling `contains()`.
--> $DIR/find_is_some_on_strs.rs:25:21
|
LL | let _ = s1[2..].find(&s2[2..]).is_some();
Expand Down

0 comments on commit a51e0ea

Please sign in to comment.