forked from rust-lang/rust-clippy
-
Notifications
You must be signed in to change notification settings - Fork 0
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 rust-lang#9692 - llogiq:mutable-key-more-arcs, r=Alexendoo
make ignored internally mutable types for `mutable-key` configurable We had some false positives where people would create their own types that had interior mutability unrelated to hash/eq. This addition lets you configure this as e.g. `arc-like-types=["bytes::Bytes"]` This fixes rust-lang#5325 by allowing users to specify the types whose innards like `Arc` should be ignored (the generic types are still checked) for the sake of detecting inner mutability. r? `@Alexendoo` --- changelog: Allow configuring types to ignore internal mutability in `mutable-key`
- Loading branch information
Showing
7 changed files
with
199 additions
and
66 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
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 @@ | ||
ignore-interior-mutability = ["mut_key::Counted"] |
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,53 @@ | ||
// compile-flags: --crate-name mut_key | ||
|
||
#![warn(clippy::mutable_key_type)] | ||
|
||
use std::cmp::{Eq, PartialEq}; | ||
use std::collections::{HashMap, HashSet}; | ||
use std::hash::{Hash, Hasher}; | ||
use std::ops::Deref; | ||
use std::sync::atomic::{AtomicUsize, Ordering}; | ||
|
||
struct Counted<T> { | ||
count: AtomicUsize, | ||
val: T, | ||
} | ||
|
||
impl<T: Clone> Clone for Counted<T> { | ||
fn clone(&self) -> Self { | ||
Self { | ||
count: AtomicUsize::new(0), | ||
val: self.val.clone(), | ||
} | ||
} | ||
} | ||
|
||
impl<T: PartialEq> PartialEq for Counted<T> { | ||
fn eq(&self, other: &Self) -> bool { | ||
self.val == other.val | ||
} | ||
} | ||
impl<T: PartialEq + Eq> Eq for Counted<T> {} | ||
|
||
impl<T: Hash> Hash for Counted<T> { | ||
fn hash<H: Hasher>(&self, state: &mut H) { | ||
self.val.hash(state); | ||
} | ||
} | ||
|
||
impl<T> Deref for Counted<T> { | ||
type Target = T; | ||
|
||
fn deref(&self) -> &T { | ||
self.count.fetch_add(1, Ordering::AcqRel); | ||
&self.val | ||
} | ||
} | ||
|
||
// This is not linted because `"mut_key::Counted"` is in | ||
// `arc_like_types` in `clippy.toml` | ||
fn should_not_take_this_arg(_v: HashSet<Counted<String>>) {} | ||
|
||
fn main() { | ||
should_not_take_this_arg(HashSet::new()); | ||
} |
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