Skip to content

incorrect Suspicious top-level unqualified call warning message #20651

Open
@xuwei-k

Description

@xuwei-k

Compiler version

3.5.0-RC1

Minimized example

scalaVersion := "3.5.0-RC1"

scalacOptions += "-explain"
package example

object A {

  def main(args: Array[String]): Unit = {
    println(new B(getClass.getName){}.x) // warn

    println(new B(getClass.getName).x) // no warn
  }
}

class B(val x: String)

Output Error/Warning message

[warn] 7 |    println(new B(getClass.getName){}.x)
[warn]   |                  ^^^^^^^^
[warn]   |                  Suspicious top-level unqualified call to getClass
[warn]   |-----------------------------------------------------------------------------
[warn]   | Explanation (enabled by `-explain`)
[warn]   |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
[warn]   | Top-level unqualified calls to AnyRef or Any methods such as getClass are
[warn]   | resolved to calls on Predef or on imported methods. This might not be what
[warn]   | you intended.
[warn]    -----------------------------------------------------------------------------
[warn] one warning found

Why this Error/Warning was not helpful

This .getClass call is not Predef.getClass because run result is example.A$

Suggested improvement

maybe don't report warn?

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:reportingError reporting including formatting, implicit suggestions, etcbetter-errorsIssues concerned with improving confusing/unhelpful diagnostic messagesitype:bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions