“Recently” Bjarne Stroustrup has said that when you use C++ concepts you should’t strive to use the minimal concepts that allow a templated function to compile, and instead you should use a coherent and organic concept that could include more than what you scrictly need. Otherwise you risk restricting too much the future implementations of your function, because a different algorithm could need a slightly different group of Concepts.
While Rust doesn’t have C++ concepts, I think those words of Stroustrup could be good for Rust too. Yet, sometimes I prefer to not over-constrain a generic function in Rust, so I’d like it to not use useless traits, like here where Debug is not used by the function:
fn quicksort<T: Copy + Ord + Debug>(a: &mut [T]) {
if a.len() < 2 { return; }
let pivot = a[a.len() / 2];
let (mut i, mut j) = (0, a.len() - 1);
loop {
while a[i] < pivot { i += 1; }
while a[j] > pivot { j -= 1; }
if i >= j { break; }
a.swap(i, j);
i += 1;
j -= 1;
}
quicksort(&mut a[.. i]);
quicksort(&mut a[i ..]);
}
“Recently” some HashMap functions have being modified to remove unnecessary Debug constraints ( https://github.com/rust-lang/rust/issues/41924 ), like here:
impl<'a, K: Debug, V: Debug> fmt::Debug for Keys<'a, K, V> {
So is it possible and a good idea to add a warning (or a lint for Clippy) that spots unnecessary trait constraints in generic functions?