-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Warn on unqualified top-level calls to Any or AnyRef methods #17449
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
Conversation
We should probably extend that to |
Done ✅ |
As motivation, I just did this on Scala 2! That was using The unreliable Scala 2 equality warning gave me a heads up, but linting all such selections seems safer. I haven't checked if |
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.
LGTM. Merge when you're happy to stop extending.
I'd like us to try a different warning. Predef is really not so special, after all. Consider this example: object O
@main def foo =
import O.*
println(getClass) This will print the I think the warning should instead be triggered by this condition:
Note that if we put the code in a class or object like this: object O
object C:
@main def foo =
import O.*
println(getClass) we get a different warning, which is what we want:
|
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.
By contrast I see much less of an issue if we write this.getClass
at the toplevel. Excluding this without excluding use of this
in package objects as a whole looks unsystematic.
So either disallow all explicit uses of this
in package objects or allow them all.
@odersky I changed to condition to: if tree.symbol.exists
&& defn.topClasses.contains(tree.symbol.owner)
&& (!ctx.owner.enclosingClass.exists || ctx.owner.enclosingClass.isPackageObject) then
report.warning(UnqualifiedCallToAnyRefMethod(tree, tree.symbol), tree) Seems to do what we want. What do you think? |
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.
Otherwise LGTM
@@ -1090,6 +1090,12 @@ object RefChecks { | |||
|
|||
end checkImplicitNotFoundAnnotation | |||
|
|||
def checkSynchronizedCall(tree: Tree)(using Context) = |
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.
Maybe rename that to checkAnyMemberCall
or something like that?
Co-Authored-By: Dale Wijnand <[email protected]> Co-Authored-By: itielsa <[email protected]>
Fixes #17266