Skip to content

Commit 2a943b8

Browse files
author
Roman Janusz
committed
analyzer reports warnings that can be suppressed with @nowarn
1 parent 906fe52 commit 2a943b8

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

commons-analyzer/src/main/scala/com/avsystem/commons/analyzer/AnalyzerRule.scala

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ package com.avsystem.commons
22
package analyzer
33

44
import java.io.{PrintWriter, StringWriter}
5-
65
import scala.tools.nsc.Global
6+
import scala.tools.nsc.Reporting.WarningCategory
77
import scala.util.control.NonFatal
88

99
abstract class AnalyzerRule(val global: Global, val name: String, defaultLevel: Level = Level.Warn) {
@@ -28,11 +28,16 @@ abstract class AnalyzerRule(val global: Global, val name: String, defaultLevel:
2828

2929
private def adjustMsg(msg: String): String = s"[AVS] $msg"
3030

31-
protected def report(pos: Position, message: String): Unit =
31+
protected final def report(
32+
pos: Position,
33+
message: String,
34+
category: WarningCategory = WarningCategory.Lint,
35+
site: Symbol = NoSymbol
36+
): Unit =
3237
level match {
3338
case Level.Off =>
3439
case Level.Info => reporter.echo(pos, adjustMsg(message))
35-
case Level.Warn => reporter.warning(pos, adjustMsg(message))
40+
case Level.Warn => currentRun.reporting.warning(pos, adjustMsg(message), category, site)
3641
case Level.Error => reporter.error(pos, adjustMsg(message))
3742
}
3843

commons-analyzer/src/main/scala/com/avsystem/commons/analyzer/ConstantDeclarations.scala

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class ConstantDeclarations(g: Global) extends AnalyzerRule(g, "constantDeclarati
88
import global._
99

1010
def analyze(unit: CompilationUnit): Unit = unit.body.foreach {
11-
case t@ValDef(mods, name, tpt, rhs)
11+
case t@ValDef(_, name, tpt, rhs)
1212
if t.symbol.hasGetter && t.symbol.owner.isEffectivelyFinal =>
1313

1414
val getter = t.symbol.getterIn(t.symbol.owner)
@@ -18,15 +18,18 @@ class ConstantDeclarations(g: Global) extends AnalyzerRule(g, "constantDeclarati
1818
case _ => false
1919
}
2020

21+
def doReport(msg: String): Unit =
22+
report(t.pos, msg, site = t.symbol)
23+
2124
val firstChar = name.toString.charAt(0)
2225
if (constantValue && (firstChar.isLower || !getter.isFinal)) {
23-
report(t.pos, "a literal-valued constant should be declared as a `final val` with an UpperCamelCase name")
26+
doReport("a literal-valued constant should be declared as a `final val` with an UpperCamelCase name")
2427
}
2528
if (!constantValue && firstChar.isUpper && !getter.isFinal) {
26-
report(t.pos, "a constant with UpperCamelCase name should be declared as a `final val`")
29+
doReport("a constant with UpperCamelCase name should be declared as a `final val`")
2730
}
2831
if (getter.isFinal && constantValue && !(tpt.tpe =:= rhs.tpe)) {
29-
report(t.pos, "a constant with a literal value should not have an explicit type annotation")
32+
doReport("a constant with a literal value should not have an explicit type annotation")
3033
}
3134
}
3235
case _ =>

0 commit comments

Comments
 (0)