Code Monkey home page Code Monkey logo

Comments (13)

llogiq avatar llogiq commented on July 21, 2024

What does that even mean? A mutable pointer to a mutable pointer?

It'd be easy to match but what should clippy suggest (apart from a visit to a good psychiatrist)?

from rust-clippy.

pythonesque avatar pythonesque commented on July 21, 2024

&mut T I guess? It's really hard to tell what the person actually wanted, you just know it probably wasn't that.

from rust-clippy.

llogiq avatar llogiq commented on July 21, 2024

Ok, so we assume that &mut &mut T is a copy'n'paste mistake, and the dev would want any of &mut &T, & &mut T or &mut T.

&mut is matched by either Ty{node: TyPtr(MutTy{ty: ref pty, mutbl:MutMutable}), ..} or Ty{node: TyRPtr(_, MutTy{ty: ref pty, mutbl:MutMutable}), ..}, where the ref pty is the reference. So if we have a fn match_mut(&Ty) -> Option(&Ty) that will do the above match and returnptyorNone, we can checkmatch_mut(ty).and_then(|ty| match_mut(ty)).is_some()(we need the closure to auto-deref theptr::P`) to find the pattern.

from rust-clippy.

llogiq avatar llogiq commented on July 21, 2024

I have a half-baked version in my tree, but it doesn't catch all cases. Simply using check_ty doesn't cut it, and there are all kinds of expressions where we need to unpack Tys.

from rust-clippy.

Manishearth avatar Manishearth commented on July 21, 2024

Uh, just unwrap the sty with ty_ptr of the ty::Ty coming out of expr_ty? in check_expr?

from rust-clippy.

llogiq avatar llogiq commented on July 21, 2024

I already did that, but it failed to catch

const this_is_deranged : &mut &mut u32 = &mut &mut 1u32;

from rust-clippy.

Manishearth avatar Manishearth commented on July 21, 2024

Why? The RHS is an expr which should be a ty_ptr(ty_ptr(ty_int)) or something

from rust-clippy.

llogiq avatar llogiq commented on July 21, 2024

In fact, it's an ExprAddrOf(MutMutable, ..ExprAddrOf(MutMutable, _)). And I found it.

from rust-clippy.

Manishearth avatar Manishearth commented on July 21, 2024

Ah, you misunderstand me.

While we do want to catch types which are &mut &mut Foo or expressions which are &mut &mut foo; that's not all we want to do. Those are pure AST checks.

We also want to catch cases where an &mut ptr is being needlessly re-&mut-ed. This is done by using expr_ty on an expression and checking its actual type. (The type info isn't part of the AST, it's part of the tcx)

To make this robust, I would suggest, in check_expr:

  • If it's &mut _, run expr_ty on the inner thingy and make sure it's not a mutable ty_ptr. If it is, then lint that reborrowing might be preferable.
  • If it's &mut &mut, do as we do now
  • If it's neither, run expr_ty on the whole expr and check that it's not a double mutable ptr (and warn if so)

from rust-clippy.

Manishearth avatar Manishearth commented on July 21, 2024

(So thanks for the lint ❤️, but there's more work to be done to close this issue 😄 )

from rust-clippy.

llogiq avatar llogiq commented on July 21, 2024

Ah, thank you for the clarification. I will extend the lint as you suggested.

from rust-clippy.

Manishearth avatar Manishearth commented on July 21, 2024

Thanks!

from rust-clippy.

llogiq avatar llogiq commented on July 21, 2024

And...done (At least I hope so).

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.