Skip to content

Commit 751cc2f

Browse files
mbovelnox213
andcommitted
Add warning for synchronized calls in value classes
Co-Authored-By: Yoonjae Jeon <[email protected]>
1 parent 48aac2c commit 751cc2f

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

compiler/src/dotty/tools/dotc/typer/RefChecks.scala

+3-1
Original file line numberDiff line numberDiff line change
@@ -1239,7 +1239,9 @@ object RefChecks {
12391239
def checkAnyRefMethodCall(tree: Tree)(using Context) =
12401240
if tree.symbol.exists
12411241
&& defn.topClasses.contains(tree.symbol.owner)
1242-
&& (!ctx.owner.enclosingClass.exists || ctx.owner.enclosingClass.isPackageObject) then
1242+
&& (!ctx.owner.enclosingClass.exists
1243+
|| ctx.owner.enclosingClass.isPackageObject
1244+
|| ctx.owner.enclosingClass.isValueClass) then
12431245
report.warning(UnqualifiedCallToAnyRefMethod(tree, tree.symbol), tree)
12441246

12451247
}

tests/warn/i17493.check

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
-- [E181] Potential Issue Warning: tests/warn/i17493.scala:3:11 --------------------------------------------------------
2+
3 | def g = synchronized { println("hello, world") } // warn
3+
| ^^^^^^^^^^^^
4+
| Suspicious top-level unqualified call to synchronized
5+
|---------------------------------------------------------------------------------------------------------------------
6+
| Explanation (enabled by `-explain`)
7+
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
8+
| Top-level unqualified calls to AnyRef or Any methods such as synchronized are
9+
| resolved to calls on Predef or on imported methods. This might not be what
10+
| you intended.
11+
---------------------------------------------------------------------------------------------------------------------

tests/warn/i17493.scala

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
//> using options -explain
2+
class A(val s: String) extends AnyVal {
3+
def g = synchronized { println("hello, world") } // warn
4+
}

0 commit comments

Comments
 (0)