Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fc4e29e

Browse files
committed
Lint dubious overload differs only in implicit
1 parent 14888ce commit fc4e29e

File tree

5 files changed

+48
-0
lines changed

5 files changed

+48
-0
lines changed

src/compiler/scala/tools/nsc/Reporting.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -661,6 +661,7 @@ object Reporting {
661661
LintIntDivToFloat,
662662
LintUniversalMethods,
663663
LintCloneable,
664+
LintOverload,
664665
LintNumericMethods
665666
= lint()
666667

src/compiler/scala/tools/nsc/settings/Warnings.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ trait Warnings {
229229
val IntDivToFloat = LintWarning("int-div-to-float", "Warn when an integer division is converted (widened) to floating point: `(someInt / 2): Double`.")
230230
val PatternShadow = LintWarning("pattern-shadow", "Pattern variable id is also a term in scope.")
231231
val CloneableObject = LintWarning("cloneable", "Modules (objects) should not be Cloneable.")
232+
val DubiousOverload = LintWarning("overload", "Overload differs only in an implicit parameter.")
232233

233234
def allLintWarnings = values.toSeq.asInstanceOf[Seq[LintWarning]]
234235
}
@@ -267,6 +268,7 @@ trait Warnings {
267268
def lintIntDivToFloat = lint.contains(IntDivToFloat)
268269
def warnPatternShadow = lint.contains(PatternShadow)
269270
def warnCloneableObject = lint.contains(CloneableObject)
271+
def warnDubiousOverload = lint.contains(DubiousOverload)
270272

271273
// The Xlint warning group.
272274
val lint = MultiChoiceSetting(

src/compiler/scala/tools/nsc/typechecker/RefChecks.scala

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,20 @@ abstract class RefChecks extends Transform {
144144
}
145145
}
146146
}
147+
if (settings.warnDubiousOverload) {
148+
val members = clazz.info.members
149+
val implicitMembers = members.filter(_.tpe match { case mt: MethodType => mt.isImplicit case _ => false })
150+
implicitMembers.foreach { sym =>
151+
val alts = clazz.info.decl(sym.name).filter(_.paramss.isEmpty).alternatives
152+
alts.foreach { alt =>
153+
val usage = if (alt.isMethod && !alt.isGetter) "Calls to parameterless" else "Usages of"
154+
val impl = sym.defString
155+
refchecksWarning(alt.pos,
156+
s"$usage $alt will be easy to mistake for calls to $impl, which has a single implicit parameter list.",
157+
WarningCategory.LintOverload)
158+
}
159+
}
160+
}
147161

148162
// Check for doomed attempt to overload applyDynamic
149163
if (clazz.isSubClass(DynamicClass))

test/files/neg/t7415.check

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
t7415.scala:10: warning: Calls to parameterless method foo will be easy to mistake for calls to def foo(implicit a: T): Int, which has a single implicit parameter list.
2+
def foo = 0
3+
^
4+
t7415.scala:14: warning: Usages of value foo will be easy to mistake for calls to def foo(implicit a: T): Int, which has a single implicit parameter list.
5+
val foo = 0
6+
^
7+
t7415.scala:18: warning: Usages of value foo will be easy to mistake for calls to def foo(implicit a: T): Int, which has a single implicit parameter list.
8+
private[this] val foo = 42
9+
^
10+
error: No warnings can be incurred under -Werror.
11+
3 warnings
12+
1 error

test/files/neg/t7415.scala

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//> using options -Werror -Xlint:overload
2+
3+
trait T
4+
5+
trait Base {
6+
def foo(implicit a: T) = 0
7+
}
8+
9+
trait Derived1 extends Base {
10+
def foo = 0
11+
}
12+
13+
trait Derived2 extends Base {
14+
val foo = 0
15+
}
16+
17+
class C extends Base {
18+
private[this] val foo = 42
19+
}

0 commit comments

Comments
 (0)