-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
trivially_copy_pass_by_ref does not consider pointers #5953
Comments
Just to make this clear. The fix is to check if the return type contains a pointer on any level and avoid linting if it does. Right? |
@mikerite That should work in most(?) cases, including the original one. But there's still a lot of complexity when considering the potential for something like this, where the pointer is returned indirectly through some arbitrary amount of structural nesting: struct PointerWrapper(*const RawPoint);
impl Point {
fn raw_wrapped(&self) -> PointerWrapper {
PointerWrapper(&self.raw)
}
} Or even this, where the pointer is not returned at all, rather it's used elsewhere by a side-effect: impl Point {
fn send_raw(&self, channel: &std::sync::mpsc::Sender) {
channel.send(&self.raw).unwrap();
}
} |
An alternative is to remove or disable the linter altogether until a safe approach can be determined? |
This is a similar issue to #2946.
Recently it was discovered that a change recommended by the
trivially_copy_pass_by_ref
lint had started causing undefined behavior in Rust-SDL2: Rust-SDL2/rust-sdl2#1020.This is the problematic change:
Point
is a simple wrapper around an ownedsys::SDL_Point
, which itself is just a simple FFI-compatible data structure. SincePoint
implementsCopy
, thetrivially_copy_pass_by_ref
lint was recommended onPoint::raw
- even though making that change causesPoint::raw
to return a pointer to an immediately-dropped copy of one of its own fields.Here's a minimal reproduction of the issue. The
Point::raw
method correctly produces a consistent pointer to the internalRawPoint
, whereas thePoint::raw_linted
method produces a different pointer on each call.This would be a bit more challenging to decide than the solution in #2951. Pointers could be present as arbitrarily nested fields of a returned struct, which would not itself be a pointer. Further, pointers could be manipulated by long-lasting side effects (e.g. sent over a channel) during a function without actually ever being returned. Arguably it is the responsibility of the author of the
unsafe
dereference site to guarantee that the pointer remains valid, but it's still unreasonable to recommend this change in these cases.Meta
cargo clippy -V
: clippy 0.0.212 (346aec9 2020-07-11)rustc -Vv
:The text was updated successfully, but these errors were encountered: