From 89af08b776d730952c40ff13b87a526bafa7fe15 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 22 Aug 2022 13:11:05 +0100 Subject: [PATCH] fix soundness bug --- .github/workflows/main.yml | 3 ++- src/lib.rs | 18 ++++++++++++++++-- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 8099e67..b1a1134 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -44,4 +44,5 @@ jobs: - uses: actions-rs/cargo@v1 with: command: clippy - args: -- -D warnings + # clippy::needless_lifetimes suggest unsound code in this crate + args: -- -D warnings -A clippy::needless_lifetimes diff --git a/src/lib.rs b/src/lib.rs index e642c67..0849df7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -486,7 +486,7 @@ impl LruCache { /// assert_eq!(cache.get_or_insert(1, ||"a"), &"a"); /// assert_eq!(cache.get_or_insert(1, ||"b"), &"a"); /// ``` - pub fn get_or_insert<'a, F>(&mut self, k: K, f: F) -> &'a V + pub fn get_or_insert<'a, F>(&'a mut self, k: K, f: F) -> &'a V where F: FnOnce() -> V, { @@ -581,7 +581,7 @@ impl LruCache { /// /// assert_eq!(cache.peek_lru(), Some((&1, &"a"))); /// ``` - pub fn peek_lru<'a>(&'_ self) -> Option<(&'a K, &'a V)> { + pub fn peek_lru<'a>(&'a self) -> Option<(&'a K, &'a V)> { if self.is_empty() { return None; } @@ -1886,3 +1886,17 @@ mod tests { assert_eq!(DROP_COUNT.load(Ordering::SeqCst), n * n * 2); } } + +/// Doctests for what should *not* compile +/// +/// ```compile_fail +/// let mut cache = lru::LruCache::::unbounded(); +/// let _: &'static u32 = cache.get_or_insert(0, || 92); +/// ``` +/// +/// ```compile_fail +/// let mut cache = lru::LruCache::::unbounded(); +/// let _: Option<(&'static u32, _)> = cache.peek_lru(); +/// let _: Option<(_, &'static u32)> = cache.peek_lru(); +/// ``` +fn _test_lifetimes() {}