Skip to content

Commit e33b637

Browse files
committed
Add quick fix to remove unnecessary .nn
1 parent 542ca19 commit e33b637

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-2
lines changed

compiler/src/dotty/tools/dotc/reporting/ErrorMessageID.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ enum ErrorMessageID(val isActive: Boolean = true) extends java.lang.Enum[ErrorMe
229229
case PointlessAppliedConstructorTypeID // errorNumber: 213
230230
case IllegalContextBoundsID // errorNumber: 214
231231
case NamedPatternNotApplicableID // errorNumber: 215
232+
case UnnecessaryNN // errorNumber: 216
232233

233234
def errorNumber = ordinal - 1
234235

compiler/src/dotty/tools/dotc/reporting/messages.scala

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3549,3 +3549,38 @@ final class NamedPatternNotApplicable(selectorType: Type)(using Context) extends
35493549
i"Named patterns cannot be used with $selectorType, because it is not a named tuple or case class"
35503550

35513551
override protected def explain(using Context): String = ""
3552+
3553+
/** @param reason The reason for the unnecessary null. The warning given to the user will be i""""Unncessary .nn: $reason"""
3554+
* @param sourcePosition The sourcePosition of the qualifier
3555+
*/
3556+
class UnnecessaryNN(reason: String, sourcePosition: SourcePosition)(using Context) extends SyntaxMsg(UnnecessaryNN) {
3557+
override def msg(using Context) = i"""Unnecessary .nn: $reason"""
3558+
3559+
override def explain(using Context) = {
3560+
val code1 = """val a: String = "foo".nn"""
3561+
val code2 = """val a: String = "foo""""
3562+
i"""With -Yexplicit-nulls, this happens when use apply .nn to a term that is already non-null.
3563+
|
3564+
|Example:
3565+
|
3566+
|$code1
3567+
|
3568+
|instead of
3569+
|
3570+
|$code2
3571+
|
3572+
|"""
3573+
}
3574+
3575+
private val nnSourcePosition = SourcePosition(sourcePosition.source, Span(sourcePosition.span.end, sourcePosition.span.end + 2, sourcePosition.span.end), sourcePosition.outer)
3576+
3577+
override def actions(using Context) =
3578+
List(
3579+
CodeAction(title = """Remove unnecessary .nn""",
3580+
description = None,
3581+
patches = List(
3582+
ActionPatch(nnSourcePosition, "")
3583+
)
3584+
)
3585+
)
3586+
}

compiler/src/dotty/tools/dotc/typer/Typer.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1093,8 +1093,8 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
10931093
if symbol.exists && symbol.owner == defn.ScalaPredefModuleClass && symbol.name == nme.nn then
10941094
tree match
10951095
case Apply(_, args) =>
1096-
if(args.head.tpe.isNotNull) then report.warning("Unnecessary .nn: qualifier is already not null", tree)
1097-
if pt.admitsNull then report.warning("Unnecessary .nn: expected type admits null", tree)
1096+
if(args.head.tpe.isNotNull) then report.warning(UnnecessaryNN("qualifier is already not null", args.head.sourcePos), tree)
1097+
if pt.admitsNull then report.warning(UnnecessaryNN("expected type admits null", args.head.sourcePos), tree)
10981098
case _ =>
10991099
}
11001100
}

0 commit comments

Comments
 (0)