Comments (10)
As a first approximation, we can warn when the self type is in collections
.
If it's easy enough to check for an .is_empty()
method, we can do that instead.
from rust-clippy.
So, I now know how to get at the type (though I'm not sure what the exact contents would be), so how do we find out if the type has an is_empty()
method?
from rust-clippy.
I assume you go through the method map? Everything regarding type check and resolution is done through the tcx (type ctxt
). Check out middle::ty
for a ton of helper functions.
from rust-clippy.
I know how to get the type definition (via expr_ty
), but I'm a bit lost on how to go from there to the MethodMap. Do I construct a MethodCall
via its expr
and use that to query the MethodMap? Also what do I do with the MethodCallee
once I have it?
from rust-clippy.
I'm not sure. Iterating through the tcx.impl_or_trait_items
entry for the defid of the given type might work.
You'll have to explore middle::ty
and ctxt
to get this I guess :)
from rust-clippy.
I've run into some wrestling with typeck, but at least I feel like I'm on the right track.
I'll see once the code compiles, anyway. 😄 However, it's getting late, so I'll resume tomorrow.
from rust-clippy.
Now it compiles, but the type lookup fails for the simple case let x = [1, 2]; if x.len() == 0 { … }
. I cannot figure out how to get from the DefLocal to a slice of its ImplOrTraitItemId
s. Probably the whole approach is wrong.
from rust-clippy.
I've never really played with this so you're basically on your own here, sorry :) Poking people in #rust-internals may help
from rust-clippy.
The problem is I'd either need to copy large swaths of typeck and patch rustc to allow me to access some private fields/methods, or just wait until the core team makes this available for lints.
The latter part opens up some of my time to try and implement some other things, so I guess I'll wait.
from rust-clippy.
The lint in #60 basically encodes that every impl that has .len ()
should have .is_empty()
, so we can get by without method lookup at the cost of possible false positives.
This is intended as a stop-gap solution, but may turn out to be good enough should we surmise that the cost of method lookup is too high.
from rust-clippy.
Related Issues (20)
- redundant_slicing suggests change that doesn't compile.
- assigning_clones: unhelpful in test code
- Incorrect `clippy::useless_attribute` for `disallowed_types`
- Warn on any panic code like `unwrap`, `expect`, `assert`, `todo`, ... HOT 2
- Clippy demands you violate ownership to satisfy `assigning_clones` HOT 3
- single_match: type are incompatible HOT 1
- `missing_panics_doc` shouldn't be triggered in constant environments
- suggestion from nonminimal_bool with integer cast creates a syntax error due to parentheses removal
- error: the 'cargo' binary, normally provided by the 'cargo' component, is not applicable to the '1.78.0-x86_64-unknown-linux-gnu' toolchain HOT 1
- Add fix for str_to_string
- `ref_as_ptr` example does not emit warnings HOT 1
- Does not infer information based on previous bitwise operations when in match statment HOT 1
- add lint for empty `vec!` invocation HOT 2
- New lint: adding a `char` to a String through `to_string()` HOT 2
- assigning_clones: suggested code contains expanded macro
- assigning_clones: makes code less readable and harder to maintain HOT 4
- `cast_possible_truncation` could leverage simple patterns
- single_element_loop: syntax error with `never patterns` ( missing parens)
- ICE when invoking `cargo clippy --fix` HOT 4
- False positive: useless_asref when used to go from `&&T` to `&T` HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from rust-clippy.