Skip to content

Commit 2a6e409

Browse files
SuperCl4sholhotak
authored andcommitted
Add quick fix to remove unnecessary .nn
1 parent 60864b3 commit 2a6e409

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
@@ -3587,3 +3587,38 @@ final class NamedPatternNotApplicable(selectorType: Type)(using Context) extends
35873587
i"Named patterns cannot be used with $selectorType, because it is not a named tuple or case class"
35883588

35893589
override protected def explain(using Context): String = ""
3590+
3591+
/** @param reason The reason for the unnecessary null. The warning given to the user will be i""""Unncessary .nn: $reason"""
3592+
* @param sourcePosition The sourcePosition of the qualifier
3593+
*/
3594+
class UnnecessaryNN(reason: String, sourcePosition: SourcePosition)(using Context) extends SyntaxMsg(UnnecessaryNN) {
3595+
override def msg(using Context) = i"""Unnecessary .nn: $reason"""
3596+
3597+
override def explain(using Context) = {
3598+
val code1 = """val a: String = "foo".nn"""
3599+
val code2 = """val a: String = "foo""""
3600+
i"""With -Yexplicit-nulls, this happens when use apply .nn to a term that is already non-null.
3601+
|
3602+
|Example:
3603+
|
3604+
|$code1
3605+
|
3606+
|instead of
3607+
|
3608+
|$code2
3609+
|
3610+
|"""
3611+
}
3612+
3613+
private val nnSourcePosition = SourcePosition(sourcePosition.source, Span(sourcePosition.span.end, sourcePosition.span.end + 2, sourcePosition.span.end), sourcePosition.outer)
3614+
3615+
override def actions(using Context) =
3616+
List(
3617+
CodeAction(title = """Remove unnecessary .nn""",
3618+
description = None,
3619+
patches = List(
3620+
ActionPatch(nnSourcePosition, "")
3621+
)
3622+
)
3623+
)
3624+
}

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

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

0 commit comments

Comments
 (0)