rust-lang / rust-lang/rust-clippy
New lint against implicit coersion of pointers to types to pointers to trait objects
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 13.5k
- Forks
- 2.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 32
Description
What it does
Catches when implicit coersion of the form Ptr<Type> -> Ptr<dyn Trait> happens, where Ptr is any pointer type.
Requires coersion to be performed via as operator.
Advantage
In dynamic world trait object may outlive its vtable if vtable is allocated in dynamic library segment and library is unloaded later.
Worse thing that it happens even if both type and trait come from another dylib dependency which stays loaded because vtables are not exported.
So author of unloadable library has to ensure that no vtables from that library can survive past the unload.
Making all vtable creation accompanied with as operator would help to find them and verify that they won't live longer than allowed.
Drawbacks
Should be disabled by default and not included in groups, probably even pedantic.
Hard to discover, the problem is not well known. I could only find one 4 years discussion rust-lang users forum.
Example
trait Foo {}
struct Bar;
impl Foo for Bar {}
fn use_foo(_: &mut dyn Foo) {}
fn make_foo() -> &'static dyn Foo {
&Bar
}
let foo: Box<Foo> = Box::new(Bar);
use_foo(&mut Bar);
Could be written as:
trait Foo {}
struct Bar;
impl Foo for Bar {}
fn use_foo(_: &mut dyn Foo) {}
fn make_foo() -> &'static dyn Foo {
&Bar as &dyn Foo
}
let foo: Box<dyn Foo> = Box::new(Bar) as Box<dyn Foo>;
use_foo(&mut Bar &mut dyn Foo);
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start from the proposed Rust examples and the stated pointer-to-trait-object coercion behavior. Define the lint's handling of all pointer types, ensure explicit as coercions are accepted, and verify that it is disabled by default and excluded from lint groups as requested.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- devtools
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100