-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
52c25e9
commit ecf0c76
Showing
5 changed files
with
132 additions
and
16 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,41 @@ | ||
use crate::utils::span_lint_and_help; | ||
use crate::utils::usage::mutated_variables; | ||
use crate::utils::{expr_or_init, is_trait_method, span_lint_and_help}; | ||
use if_chain::if_chain; | ||
use rustc_hir as hir; | ||
use rustc_lint::LateContext; | ||
use rustc_span::sym; | ||
|
||
use super::SUSPICIOUS_MAP; | ||
|
||
pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>) { | ||
span_lint_and_help( | ||
cx, | ||
SUSPICIOUS_MAP, | ||
expr.span, | ||
"this call to `map()` won't have an effect on the call to `count()`", | ||
None, | ||
"make sure you did not confuse `map` with `filter` or `for_each`", | ||
); | ||
pub fn check<'tcx>( | ||
cx: &LateContext<'tcx>, | ||
expr: &hir::Expr<'_>, | ||
map_args: &[hir::Expr<'_>], | ||
count_args: &[hir::Expr<'_>], | ||
) { | ||
if_chain! { | ||
if let [count_recv] = count_args; | ||
if let [_, map_arg] = map_args; | ||
if is_trait_method(cx, count_recv, sym::Iterator); | ||
let closure = expr_or_init(cx, map_arg); | ||
if let Some(body_id) = cx.tcx.hir().maybe_body_owned_by(closure.hir_id); | ||
let closure_body = cx.tcx.hir().body(body_id); | ||
if !cx.typeck_results().expr_ty(&closure_body.value).is_unit(); | ||
then { | ||
if let Some(map_mutated_vars) = mutated_variables(&closure_body.value, cx) { | ||
// A variable is used mutably inside of the closure. Suppress the lint. | ||
if !map_mutated_vars.is_empty() { | ||
return; | ||
} | ||
} | ||
span_lint_and_help( | ||
cx, | ||
SUSPICIOUS_MAP, | ||
expr.span, | ||
"this call to `map()` won't have an effect on the call to `count()`", | ||
None, | ||
"make sure you did not confuse `map` with `filter` or `for_each`", | ||
); | ||
} | ||
} | ||
} |
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