Skip to content

Commit

Permalink
Since the problem in SI-6758 is fixed, it's ok to move checking for u…
Browse files Browse the repository at this point in the history
…nused imports to Analyzer. This allows the check to be used in the IDE.
  • Loading branch information
vigdorchik committed Mar 5, 2013
1 parent ce32c1a commit 1666f6e
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 28 deletions.
14 changes: 0 additions & 14 deletions src/compiler/scala/tools/nsc/symtab/classfile/Pickler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,6 @@ abstract class Pickler extends SubComponent {
def newPhase(prev: Phase): StdPhase = new PicklePhase(prev)

class PicklePhase(prev: Phase) extends StdPhase(prev) {
override def run() {
super.run()
// This is run here rather than after typer because I found
// some symbols - usually annotations, possibly others - had not
// yet performed the necessary symbol lookup, leading to
// spurious claims of unusedness.
if (settings.lint.value) {
log("Clearing recorded import selectors.")
analyzer.clearUnusedImports()
}
}

def apply(unit: CompilationUnit) {
def pickle(tree: Tree) {
def add(sym: Symbol, pickle: Pickle) = {
Expand Down Expand Up @@ -83,8 +71,6 @@ abstract class Pickler extends SubComponent {
}

pickle(unit.body)
if (settings.lint.value)
analyzer.warnUnusedImports(unit)
}
}

Expand Down
19 changes: 8 additions & 11 deletions src/compiler/scala/tools/nsc/typechecker/Contexts.scala
Original file line number Diff line number Diff line change
Expand Up @@ -51,20 +51,17 @@ trait Contexts { self: Analyzer =>
private lazy val allImportInfos =
mutable.Map[CompilationUnit, List[ImportInfo]]() withDefaultValue Nil

def clearUnusedImports() {
allUsedSelectors.clear()
allImportInfos.clear()
}
def warnUnusedImports(unit: CompilationUnit) = {
val imps = allImportInfos(unit).reverse.distinct

for (imp <- imps) {
val used = allUsedSelectors(imp)
def isMask(s: ImportSelector) = s.name != nme.WILDCARD && s.rename == nme.WILDCARD
for (imps <- allImportInfos.remove(unit)) {
for (imp <- imps.reverse.distinct) {
val used = allUsedSelectors(imp)
def isMask(s: ImportSelector) = s.name != nme.WILDCARD && s.rename == nme.WILDCARD

imp.tree.selectors filterNot (s => isMask(s) || used(s)) foreach { sel =>
unit.warning(imp posOf sel, "Unused import")
imp.tree.selectors filterNot (s => isMask(s) || used(s)) foreach { sel =>
unit.warning(imp posOf sel, "Unused import")
}
}
allUsedSelectors --= imps
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,8 @@ trait TypeDiagnostics {
}

def apply(unit: CompilationUnit) = {
warnUnusedImports(unit)

val p = new UnusedPrivates
p traverse unit.body
val unused = p.unusedTerms
Expand Down
6 changes: 3 additions & 3 deletions test/files/neg/warn-unused-imports.check
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ warn-unused-imports.scala:13: warning: it is not recommended to define classes/o
If possible, define class A in package p2 instead.
class A
^
warn-unused-imports.scala:99: warning: local trait Warn is never used
trait Warn { // warn about unused local trait for good measure
^
warn-unused-imports.scala:57: warning: Unused import
import p1.A // warn
^
Expand Down Expand Up @@ -39,6 +36,9 @@ warn-unused-imports.scala:98: warning: Unused import
warn-unused-imports.scala:118: warning: Unused import
import p1.A // warn
^
warn-unused-imports.scala:99: warning: local trait Warn is never used
trait Warn { // warn about unused local trait for good measure
^
error: No warnings can be incurred under -Xfatal-warnings.
13 warnings found
one error found

0 comments on commit 1666f6e

Please sign in to comment.