Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upRestriction Lint: Literal Patterns #2850
Comments
This comment has been minimized.
This comment has been minimized.
|
If all patterns in a match on a If all patterns in a match on a The rest of your lint suggestion only makes sense to me as a lint forbidding any binding of (mutable?) reference type. You have the exact same problem if someone writes If you are doing |
This comment has been minimized.
This comment has been minimized.
I agree.
I also agree here. But that doesn't cover mixed bindings.
I would disagree with this because:
I didn't suggest that. Like I said above, that problem is more something for naming in APIs, which isn't easily lintable.
I'm not sure I understand you here. The issue is you're having a |
This comment has been minimized.
This comment has been minimized.
yes
So the suggested restriction lint should catch
Since function arguments are also bindings, the lint will also trigger on mutable references to functions. This means you are getting into very functional territory, because you are essentially banning non-local mutation. I can totally get behind that, but we need to discuss the implications. I suggest you open three issues: one for each of the general lints and one for the functional restriction lint. |
This comment has been minimized.
This comment has been minimized.
|
You've lost me there. I don't think I've ever used a "mutable reference to a function". Do you mean closures? I don't understand how this hangs together. I'm after a way to locally require that patterns specify how they bind, instead of adapting to whatever type of value they got. |
This comment has been minimized.
This comment has been minimized.
|
Sorry, that was badly worded. I meant a function having an argument of Patterns are not special. The reason we have match ergonomics now is because everything else was already behaving this way. So either all bindings of mutable reference type are problematic or none. I don't see how a match pattern binding is different from any other binding. |
This comment has been minimized.
This comment has been minimized.
|
@oli-obk I agree it would be great if there were ways to combat that. I feel it's different for me because it's a very specific scope where this lint would be helpful for me. This is basically for when I have big state machines taking things apart with Using an explicit matching mode makes it easier to see those semantics of the event processor (or whatever). The lint's primary function would be to enforce consistency inside a So, even if we had a lint that tells me that I should name a field, variable, type, or function better to indiciate ownership transfers and mutable access, I'd still want to have a lint like this for complex matching logic functions (they're usually big but mostly do matching) just to keep consistency for how things are taken apart in that logic. |
This comment has been minimized.
This comment has been minimized.
|
Since default binding modes have been shown to easily trigger soundness bugs, linting them seems like not the worst idea ever. |
This comment has been minimized.
This comment has been minimized.
|
Linting rustc bugs is out of scope for clippy. The effort is better spent fixing rustc. The way to resolve this issue is to lint all bindings of |
phaylon commentedJun 17, 2018
It can sometimes be desirable to not use the default binding modes, but instead write the patterns to literally match the types they are matching. Example:
instead of
Situations I ran into where this would help are:
In all these cases a literal pattern match has the information more locally available:
ref mut xinstead ofxand somewhere laterlet x = &*xto limit mutability.xinstead ofxand later*xfor copying values out. This also prevents accidental later mutation whenxis a&mut _.More specifically, this would be useful for complex state machines and event processing decisions where you'd want ownership and mutability to be at their most self-documenting level. A restriction lint can be used to catch accidental adaptive matching from oneself or outside contributors.
The code making me hope for such a lint was a stream based event processing library where many nested parts kept individual but sometimes also semantically connected state. Unwanted mutations and non-evident copy semantics for flags and counters were the biggest time sinks when debugging, since the problems only became visible in edge cases once the whole stream was processed and combined into a whole.
Having these markers at the point of destructuring was a big help discovering these problem causes and I'd love for a way to uphold the information-locality for those code parts when multiple contributors are working on code.