New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add -Xlint:pattern-shadow
to lint pattern varids which are backquotable
#8806
Conversation
Also patdefs, cf scala/bug#7530 |
Clunky lint would be nicer as lightweight 3rd party linter and lint suppression via |
Was I supposed to revisit this? Summer '20 was too early in pandemic to be morbidly depressed. My system of adding annotations in the PR title must have broken down. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks useful - did you try it on the compiler / library to see how often it fires?
@@ -4628,6 +4628,9 @@ trait Typers extends Adaptations with Tags with TypersTracking with PatternTyper | |||
tree setSymbol sym setType sym.tpeHK | |||
|
|||
case Bind(name: TermName, body) => | |||
if (settings.warnPatternShadow && !tree.hasAttachment[NoWarnAttachment.type] && context.isNameInScope(name)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why the NoWarn
filter?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All questions will be answered after I finish linting "literal boolean args should be named". That will be after I have coffee.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The attachment represents user code
case x @ _ =>
for which we don't want to propose
case `x` @ _ =>
Backquotes in that case is useful for
case `type` @ _ =>
I have a second branch where I may have fixed some project source, because this feels familiar:
where
Why doesn't this come up as an obvious reason not to name all values with lower case? The other interesting aspect is that the var is unused on the RHS. I may have already considered that as a criterion: surely if I use the var, I intended to use it? unless I unintended the other element. |
818aa90
to
2e18a11
Compare
Forgot to restarr locally before pushing, d'oh. This day is already much too long, there will be more days. I almost wrote morose days. I noticed this PR began on my birthday, two days before lockdown. An age ago, so to speak. Edit: I recognize Also |
2e18a11
to
d632c06
Compare
src/compiler/scala/tools/nsc/symtab/classfile/ClassfileParser.scala
Outdated
Show resolved
Hide resolved
|
Now I want it to see that |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I noticed that it's annoying to say "shadowed by value x", it should at least give a line number
👍 and / or tell the owner even if x
is a local.
I think it would be nice to say Otherwise, just add line number for locals and parameters. Hopefully, user can locate members. Note that is not necessary to say |
If the shadowed element appears anywhere in the selector expression, don't warn about the shadowing pattern variable. This means that there might be no type relationship between the two symbols, and that elements of tuples might be swapped, i.e., there is no ordering relationship between arguments in the selector and in the pattern. For example, `(x, y) match { case (y, x) => }`. For example, `t.toString match { case t => }`.
686bcdf
to
e534c44
Compare
My phrase for "shows up in scrutinee" is "implicated in selector expression". I took your suggestion and scrapped the restrictions around the selector at the cost of more false negatives. This is clearly wrong:
The previous enforcement was that the result of I tried adding a type condition on the pat var and the shadowee, but the typical challenge is
Maybe it's enough to note type of x in the wrapped type. That is, like the current tweak, it's enough if it appears somewhere, and we won't be picky about the shape. Really we're saying, x should have some type relationship to what it shadows, if not conforming then at least embedding. |
@lrytz that was my last swing for now. You may not like that I chose not to clutter up the help text. Currently, the lint passes the "no-brainer" test (not noisy and helpful if it warns). Since everyone asks about warnings on Scala 2 vs Scala 3, not to mention compiler options, what is needed is that doc, which would explain the options operationally. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great, thank you for going through the many iterations!
-Xlint:pattern-shadow
to lint pattern varids which are backquotable
Lint
-Xlint:pattern-shadow
warns when a pattern has a variable that may have been intended to match a value in scope using "backtick" notation.For the common idiom of "refining" a value through pattern matching, there is no warning if the value in scope is used in the selector expression:
This exception is permissive. For example, tuple arguments need not "align" in the pattern:
Fixes scala/bug#11850