You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Here is the essence of a bug that took me several hours to find in a larger code base. The code originally came from a much larger, multi-threaded function that fed data into a machine learning system. Instead of asserting, it just produced inputs that subtly changed other outputs.
In our project we had the following lints and warning enabled:
#![forbid(unsafe_code)]#![warn(clippy::all)]#![warn(clippy::nursery)]#![warn(clippy::pedantic)]#![allow(clippy::inline_always)]// Globally allow "inline" because we do it too much.#![allow(clippy::module_name_repetitions)]// We have too many of these
Analysis
The bug comes from combining a struct S {} that contains an Arc with the vec![T; N] macro. The official documentation even warns against this sort of behavior:
This will use clone to duplicate an expression, so one should be careful using this with types having a nonstandard Clone implementation. For example, vec![Rc::new(1); 5] will create a vector of five references to the same boxed integer value, not five references pointing to independently boxed integers.
However, my intuition is that most people are so used to vec![S; 10] meaning "give me 10 independent instances of S" that this 'nonstandard case' will be somewhat surprising.
Suggestion
This bug could have been avoided if Clippy warned against known 'nonstandard' Clone implementations used in vec!. For starters, that could be any call to vec![T; N], where T contains an Arc or Rc.
The text was updated successfully, but these errors were encountered:
If the lint isn't 100% accurate I'd probably rather have a false positive I can silence, than having subtle errors caused by the combination of two innocent constructs possibly far apart.
On the other hand, I wouldn't want too many false positives, and I don't know how prevalent those constructs are.
Background
Here is the essence of a bug that took me several hours to find in a larger code base. The code originally came from a much larger, multi-threaded function that fed data into a machine learning system. Instead of asserting, it just produced inputs that subtly changed other outputs.
In our project we had the following lints and warning enabled:
Analysis
The bug comes from combining a
struct S {}
that contains anArc
with thevec![T; N]
macro. The official documentation even warns against this sort of behavior:However, my intuition is that most people are so used to
vec![S; 10]
meaning "give me 10 independent instances of S" that this 'nonstandard case' will be somewhat surprising.Suggestion
This bug could have been avoided if Clippy warned against known 'nonstandard' Clone implementations used in
vec!
. For starters, that could be any call tovec![T; N]
, whereT
contains anArc
orRc
.The text was updated successfully, but these errors were encountered: