Skip to content

Commit 8976ebc

Browse files
committed
fix: map_entry FP on struct member
1 parent e6d9641 commit 8976ebc

File tree

3 files changed

+64
-2
lines changed

3 files changed

+64
-2
lines changed

clippy_lints/src/entry.rs

+18-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use core::fmt::{self, Write};
88
use rustc_errors::Applicability;
99
use rustc_hir::hir_id::HirIdSet;
1010
use rustc_hir::intravisit::{Visitor, walk_expr};
11-
use rustc_hir::{Block, Expr, ExprKind, HirId, Pat, Stmt, StmtKind, UnOp};
11+
use rustc_hir::{Block, Expr, ExprKind, HirId, Pat, QPath, Stmt, StmtKind, UnOp};
1212
use rustc_lint::{LateContext, LateLintPass};
1313
use rustc_session::declare_lint_pass;
1414
use rustc_span::{DUMMY_SP, Span, SyntaxContext, sym};
@@ -500,7 +500,7 @@ impl<'tcx> Visitor<'tcx> for InsertSearcher<'_, 'tcx> {
500500
self.visit_non_tail_expr(insert_expr.value);
501501
self.is_single_insert = is_single_insert;
502502
},
503-
_ if SpanlessEq::new(self.cx).eq_expr(self.map, expr) => {
503+
_ if is_map_used(self.cx, self.map, expr) => {
504504
self.is_map_used = true;
505505
},
506506
_ => match expr.kind {
@@ -562,6 +562,22 @@ impl<'tcx> Visitor<'tcx> for InsertSearcher<'_, 'tcx> {
562562
}
563563
}
564564

565+
fn is_map_used(cx: &LateContext<'_>, map: &Expr<'_>, expr: &Expr<'_>) -> bool {
566+
if SpanlessEq::new(cx).eq_expr(map, expr) {
567+
return true;
568+
}
569+
570+
match map.kind {
571+
ExprKind::Field(inner, _) | ExprKind::Index(inner, _, _) => is_map_used(cx, inner, expr),
572+
ExprKind::Path(QPath::Resolved(_, map_path))
573+
if let ExprKind::Path(QPath::Resolved(_, expr_path)) = expr.kind =>
574+
{
575+
SpanlessEq::new(cx).eq_path(map_path, expr_path)
576+
},
577+
_ => false,
578+
}
579+
}
580+
565581
struct InsertSearchResults<'tcx> {
566582
edits: Vec<Edit<'tcx>>,
567583
allow_insert_closure: bool,

tests/ui/entry.fixed

+23
Original file line numberDiff line numberDiff line change
@@ -186,4 +186,27 @@ fn issue12489(map: &mut HashMap<u64, u64>) -> Option<()> {
186186
Some(())
187187
}
188188

189+
mod issue13934 {
190+
use std::collections::HashMap;
191+
192+
struct Member {}
193+
194+
pub struct Foo {
195+
members: HashMap<u8, Member>,
196+
}
197+
198+
impl Foo {
199+
pub fn should_also_not_cause_lint(&mut self, input: u8) {
200+
if self.members.contains_key(&input) {
201+
todo!();
202+
} else {
203+
self.other();
204+
self.members.insert(input, Member {});
205+
}
206+
}
207+
208+
fn other(&self) {}
209+
}
210+
}
211+
189212
fn main() {}

tests/ui/entry.rs

+23
Original file line numberDiff line numberDiff line change
@@ -190,4 +190,27 @@ fn issue12489(map: &mut HashMap<u64, u64>) -> Option<()> {
190190
Some(())
191191
}
192192

193+
mod issue13934 {
194+
use std::collections::HashMap;
195+
196+
struct Member {}
197+
198+
pub struct Foo {
199+
members: HashMap<u8, Member>,
200+
}
201+
202+
impl Foo {
203+
pub fn should_also_not_cause_lint(&mut self, input: u8) {
204+
if self.members.contains_key(&input) {
205+
todo!();
206+
} else {
207+
self.other();
208+
self.members.insert(input, Member {});
209+
}
210+
}
211+
212+
fn other(&self) {}
213+
}
214+
}
215+
193216
fn main() {}

0 commit comments

Comments
 (0)