-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #12339 - GuillaumeGomez:add-unnecessary_get_then_check,…
… r=llogiq Add new `unnecessary_get_then_check` lint No issue linked to this as far as I can see. It's a lint I discovered that could be added when I worked on another lint. r? `@llogiq` changelog: Add new `unnecessary_get_then_check` lint
- Loading branch information
Showing
11 changed files
with
273 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_and_then}; | ||
use clippy_utils::source::snippet_opt; | ||
use clippy_utils::ty::is_type_diagnostic_item; | ||
|
||
use rustc_errors::Applicability; | ||
use rustc_hir::{Expr, ExprKind}; | ||
use rustc_lint::LateContext; | ||
use rustc_middle::ty::Ty; | ||
use rustc_span::{sym, Span}; | ||
|
||
use super::UNNECESSARY_GET_THEN_CHECK; | ||
|
||
fn is_a_std_set_type(cx: &LateContext<'_>, ty: Ty<'_>) -> bool { | ||
is_type_diagnostic_item(cx, ty, sym::HashSet) || is_type_diagnostic_item(cx, ty, sym::BTreeSet) | ||
} | ||
|
||
fn is_a_std_map_type(cx: &LateContext<'_>, ty: Ty<'_>) -> bool { | ||
is_type_diagnostic_item(cx, ty, sym::HashMap) || is_type_diagnostic_item(cx, ty, sym::BTreeMap) | ||
} | ||
|
||
pub(super) fn check( | ||
cx: &LateContext<'_>, | ||
call_span: Span, | ||
get_call: &Expr<'_>, | ||
get_caller: &Expr<'_>, | ||
arg: &Expr<'_>, | ||
is_some: bool, | ||
) { | ||
let caller_ty = cx.typeck_results().expr_ty(get_caller); | ||
|
||
let is_set = is_a_std_set_type(cx, caller_ty); | ||
let is_map = is_a_std_map_type(cx, caller_ty); | ||
|
||
if !is_set && !is_map { | ||
return; | ||
} | ||
let ExprKind::MethodCall(path, _, _, get_call_span) = get_call.kind else { | ||
return; | ||
}; | ||
let both_calls_span = get_call_span.with_hi(call_span.hi()); | ||
if let Some(snippet) = snippet_opt(cx, both_calls_span) | ||
&& let Some(arg_snippet) = snippet_opt(cx, arg.span) | ||
{ | ||
let generics_snippet = if let Some(generics) = path.args | ||
&& let Some(generics_snippet) = snippet_opt(cx, generics.span_ext) | ||
{ | ||
format!("::{generics_snippet}") | ||
} else { | ||
String::new() | ||
}; | ||
let suggestion = if is_set { | ||
format!("contains{generics_snippet}({arg_snippet})") | ||
} else { | ||
format!("contains_key{generics_snippet}({arg_snippet})") | ||
}; | ||
if is_some { | ||
span_lint_and_sugg( | ||
cx, | ||
UNNECESSARY_GET_THEN_CHECK, | ||
both_calls_span, | ||
&format!("unnecessary use of `{snippet}`"), | ||
"replace it with", | ||
suggestion, | ||
Applicability::MaybeIncorrect, | ||
); | ||
} else if let Some(caller_snippet) = snippet_opt(cx, get_caller.span) { | ||
let full_span = get_caller.span.with_hi(call_span.hi()); | ||
|
||
span_lint_and_then( | ||
cx, | ||
UNNECESSARY_GET_THEN_CHECK, | ||
both_calls_span, | ||
&format!("unnecessary use of `{snippet}`"), | ||
|diag| { | ||
diag.span_suggestion( | ||
full_span, | ||
"replace it with", | ||
format!("{}{caller_snippet}.{suggestion}", if is_some { "" } else { "!" }), | ||
Applicability::MaybeIncorrect, | ||
); | ||
}, | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#![warn(clippy::unnecessary_get_then_check)] | ||
|
||
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet}; | ||
|
||
fn main() { | ||
let s: HashSet<String> = HashSet::new(); | ||
let _ = s.contains("a"); //~ ERROR: unnecessary use of `get("a").is_some()` | ||
let _ = !s.contains("a"); //~ ERROR: unnecessary use of `get("a").is_none()` | ||
|
||
let s: HashMap<String, ()> = HashMap::new(); | ||
let _ = s.contains_key("a"); //~ ERROR: unnecessary use of `get("a").is_some()` | ||
let _ = !s.contains_key("a"); //~ ERROR: unnecessary use of `get("a").is_none()` | ||
|
||
let s: BTreeSet<String> = BTreeSet::new(); | ||
let _ = s.contains("a"); //~ ERROR: unnecessary use of `get("a").is_some()` | ||
let _ = !s.contains("a"); //~ ERROR: unnecessary use of `get("a").is_none()` | ||
|
||
let s: BTreeMap<String, ()> = BTreeMap::new(); | ||
let _ = s.contains_key("a"); //~ ERROR: unnecessary use of `get("a").is_some()` | ||
let _ = !s.contains_key("a"); //~ ERROR: unnecessary use of `get("a").is_none()` | ||
|
||
// Import to check that the generic annotations are kept! | ||
let s: HashSet<String> = HashSet::new(); | ||
let _ = s.contains::<str>("a"); //~ ERROR: unnecessary use of `get::<str>("a").is_some()` | ||
let _ = !s.contains::<str>("a"); //~ ERROR: unnecessary use of `get::<str>("a").is_none()` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#![warn(clippy::unnecessary_get_then_check)] | ||
|
||
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet}; | ||
|
||
fn main() { | ||
let s: HashSet<String> = HashSet::new(); | ||
let _ = s.get("a").is_some(); //~ ERROR: unnecessary use of `get("a").is_some()` | ||
let _ = s.get("a").is_none(); //~ ERROR: unnecessary use of `get("a").is_none()` | ||
|
||
let s: HashMap<String, ()> = HashMap::new(); | ||
let _ = s.get("a").is_some(); //~ ERROR: unnecessary use of `get("a").is_some()` | ||
let _ = s.get("a").is_none(); //~ ERROR: unnecessary use of `get("a").is_none()` | ||
|
||
let s: BTreeSet<String> = BTreeSet::new(); | ||
let _ = s.get("a").is_some(); //~ ERROR: unnecessary use of `get("a").is_some()` | ||
let _ = s.get("a").is_none(); //~ ERROR: unnecessary use of `get("a").is_none()` | ||
|
||
let s: BTreeMap<String, ()> = BTreeMap::new(); | ||
let _ = s.get("a").is_some(); //~ ERROR: unnecessary use of `get("a").is_some()` | ||
let _ = s.get("a").is_none(); //~ ERROR: unnecessary use of `get("a").is_none()` | ||
|
||
// Import to check that the generic annotations are kept! | ||
let s: HashSet<String> = HashSet::new(); | ||
let _ = s.get::<str>("a").is_some(); //~ ERROR: unnecessary use of `get::<str>("a").is_some()` | ||
let _ = s.get::<str>("a").is_none(); //~ ERROR: unnecessary use of `get::<str>("a").is_none()` | ||
} |
Oops, something went wrong.