Skip to content

Commit

Permalink
Tweak warning
Browse files Browse the repository at this point in the history
  • Loading branch information
som-snytt committed Jan 13, 2025
1 parent 64034cb commit dbd7c5b
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 3 deletions.
5 changes: 2 additions & 3 deletions compiler/src/dotty/tools/dotc/transform/CheckUnused.scala
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,7 @@ class CheckUnused private (phaseMode: PhaseMode, suffix: String) extends MiniPha
refInfos.inlined.push(tree.call.srcPos)
ctx
override def transformInlined(tree: Inlined)(using Context): tree.type =
//val _ = refInfos.inlined.pop()
val prev = refInfos.inlined.pop()
val _ = refInfos.inlined.pop()
if !tree.call.isEmpty && phaseMode.eq(PhaseMode.Aggregate) then
transformAllDeep(tree.call)
tree
Expand Down Expand Up @@ -466,7 +465,7 @@ object CheckUnused:
if sym.isLocalToBlock then
if ctx.settings.WunusedHas.locals && sym.is(Mutable) && !infos.asss(sym) then
warnAt(pos)(UnusedSymbol.unsetLocals)
else if sym.isAllOf(Private | Mutable) && !infos.asss(sym) then
else if ctx.settings.WunusedHas.privates && sym.isAllOf(Private | Mutable) && !infos.asss(sym) then
warnAt(pos)(UnusedSymbol.unsetPrivates)
else if sym.is(Private, butNot = ParamAccessor) then
if ctx.settings.WunusedHas.privates
Expand Down
4 changes: 4 additions & 0 deletions tests/warn/i15503d.scala
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,7 @@ object `mutable patvar in for`:
for x <- xs; y = x + 1 if y > 10 yield
var z :: Nil = y :: Nil: @unchecked // warn
z + 10

class `unset var requires -Wunused`:
private var i = 0 // no warn as we didn't ask for it
def f = println(i)
39 changes: 39 additions & 0 deletions tests/warn/i18564.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@

//> using option -Wunused:imports

import scala.compiletime.*
import scala.deriving.*

trait Foo

trait HasFoo[A]:
/** true if A contains a Foo */
val hasFoo: Boolean

// given instances that need to be imported to be in scope
object HasFooInstances:
given defaultHasFoo[A]: HasFoo[A] with
val hasFoo: Boolean = false
given HasFoo[Foo] with
val hasFoo: Boolean = true

object HasFooDeriving:

inline private def tupleHasFoo[T <: Tuple]: Boolean =
inline erasedValue[T] match
case _: EmptyTuple => false
case _: (t *: ts) => summonInline[HasFoo[t]].hasFoo || tupleHasFoo[ts]

inline def deriveHasFoo[T](using p: Mirror.ProductOf[T]): HasFoo[T] =
// falsely reported as unused even though it has influence on this code
import HasFooInstances.given // no warn at inline method
val pHasFoo = tupleHasFoo[p.MirroredElemTypes]
new HasFoo[T]: // warn New anonymous class definition will be duplicated at each inline site
val hasFoo: Boolean = pHasFoo

/* the import is used upon inline elaboration
object Test:
import HasFooDeriving.*
case class C(x: Foo, y: Int)
def f: HasFoo[C] = deriveHasFoo[C]
*/
13 changes: 13 additions & 0 deletions tests/warn/i19252.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//> using options -Wunused:all
object Deps:
trait D1
object D2
end Deps

object Bug:
import Deps.D1 // no warn

class Cl(d1: D1):
import Deps.*
def f = (d1, D2)
end Bug
11 changes: 11 additions & 0 deletions tests/warn/i20520.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

//> using option -Wunused:all

@main def run =
val veryUnusedVariable: Int = value // warn local

package i20520:
private def veryUnusedMethod(x: Int): Unit = println() // warn param
private val veryUnusedVariableToplevel: Unit = println() // package members are accessible under separate compilation

def value = 42
7 changes: 7 additions & 0 deletions tests/warn/i20951.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//> using options -Wunused:all
object Foo {
val dummy = 42
def f(): Unit = Option(1).map((x: Int) => dummy) // warn
def g(): Unit = Option(1).map((x: Int) => ???) // warn
def main(args: Array[String]): Unit = {}
}

0 comments on commit dbd7c5b

Please sign in to comment.