Giter Site home page Giter Site logo

container.len() == 0 about rust-clippy HOT 10 CLOSED

rust-lang avatar rust-lang commented on May 20, 2024
container.len() == 0

from rust-clippy.

Comments (10)

lambda-fairy avatar lambda-fairy commented on May 20, 2024

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.

llogiq avatar llogiq commented on May 20, 2024

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.

Manishearth avatar Manishearth commented on May 20, 2024

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.

llogiq avatar llogiq commented on May 20, 2024

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.

Manishearth avatar Manishearth commented on May 20, 2024

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.

llogiq avatar llogiq commented on May 20, 2024

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.

llogiq avatar llogiq commented on May 20, 2024

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 ImplOrTraitItemIds. Probably the whole approach is wrong.

from rust-clippy.

Manishearth avatar Manishearth commented on May 20, 2024

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.

llogiq avatar llogiq commented on May 20, 2024

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.

llogiq avatar llogiq commented on May 20, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.