From eeec8d323894d9015b586a94db7497c2080be8c2 Mon Sep 17 00:00:00 2001 From: Simon Tatham Date: Fri, 5 Jan 2024 23:31:21 +0000 Subject: [PATCH] Clippy fix: borrow the HashMap Entry we want to insert into. I hadn't known about this method of HashMap at all: you can test whether a key exists, in such a way that you also get a reference to the slot you'd want to insert it into if it didn't. Then you don't need the two hash lookups you would with the more obvious approach of separately testing for contains_key and then maybe inserting. --- src/file.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/file.rs b/src/file.rs index f0beffa..39e5a60 100644 --- a/src/file.rs +++ b/src/file.rs @@ -1,7 +1,7 @@ use itertools::Itertools; use regex::Regex; use std::cmp::{min, max}; -use std::collections::{HashMap, HashSet}; +use std::collections::{HashMap, HashSet, hash_map}; use super::activity_stack::{ NonUtilityActivity, UtilityActivity, OverlayActivity, @@ -346,7 +346,7 @@ impl File { fn ensure_item_rendered(&mut self, index: isize, w: usize) -> &Vec { - if !self.rendered.contains_key(&index) { + if let hash_map::Entry::Vacant(e) = self.rendered.entry(index) { let mut lines = Vec::new(); let highlight = match self.ui_mode { @@ -369,7 +369,7 @@ impl File { } } - self.rendered.insert(index, lines); + e.insert(lines); } self.rendered.get(&index).expect("We just made sure this was present") -- 2.30.2