Skip to content

improvement: Suggest to add using as a code action #23079

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion compiler/src/dotty/tools/dotc/reporting/Message.scala
Original file line number Diff line number Diff line change
Expand Up @@ -424,12 +424,17 @@ trait NoDisambiguation extends Message:
withoutDisambiguation()

/** The fallback `Message` containing no explanation and having no `kind` */
final class NoExplanation(msgFn: Context ?=> String)(using Context) extends Message(ErrorMessageID.NoExplanationID) {
final class NoExplanation(msgFn: Context ?=> String, actions: List[CodeAction] = List.empty)(using Context) extends Message(ErrorMessageID.NoExplanationID) {
def msg(using Context): String = msgFn
def explain(using Context): String = ""
val kind: MessageKind = MessageKind.NoKind

override def actions(using Context): List[CodeAction] = actions

override def toString(): String = msg

def withActions(actions: CodeAction*): NoExplanation =
new NoExplanation(msgFn, actions.toList)
}

/** The extractor for `NoExplanation` can be used to check whether any error
Expand Down
23 changes: 17 additions & 6 deletions compiler/src/dotty/tools/dotc/typer/Migrations.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import NameKinds.ContextBoundParamName
import rewrites.Rewrites.patch
import util.Spans.Span
import rewrites.Rewrites
import dotty.tools.dotc.rewrites.Rewrites.ActionPatch
import dotty.tools.dotc.util.SourcePosition

/** A utility trait containing source-dependent deprecation messages
* and migrations.
Expand Down Expand Up @@ -139,14 +141,23 @@ trait Migrations:
val rewriteMsg =
if hasParentheses then
Message.rewriteNotice("This code", mversion.patchFrom)
else
""
report.errorOrMigrationWarning(
else ""
val message =
em"""Implicit parameters should be provided with a `using` clause.$rewriteMsg
|To disable the warning, please use the following option:
|To disable the warning, please use the following option:
| "-Wconf:msg=Implicit parameters should be provided with a `using` clause:s"
|""",
pt.args.head.srcPos, mversion)
|"""
val codeAction = CodeAction(
title = "Add `using` clause",
description = None,
patches = List(ActionPatch(pt.args.head.startPos.sourcePos, "using "))
)
val withActions = message.withActions(codeAction)
report.errorOrMigrationWarning(
withActions,
pt.args.head.srcPos,
mversion
)
if hasParentheses && mversion.needsPatch then
patch(Span(pt.args.head.span.start), "using ")
end implicitParams
Expand Down
13 changes: 13 additions & 0 deletions compiler/test/dotty/tools/dotc/reporting/CodeActionTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,19 @@ class CodeActionTest extends DottyTest:
|""".stripMargin
)

@Test def addUsingClause =
checkCodeAction(
"""|object Test:
| def foo(implicit a: Int) = a
| foo(123)
|""".stripMargin,
"Add `using` clause",
"""|object Test:
| def foo(implicit a: Int) = a
| foo(using 123)
|""".stripMargin
)

@Test def insertMissingCases =
checkCodeAction(
code =
Expand Down
2 changes: 1 addition & 1 deletion tests/neg/i22440.check
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
| ^
| Implicit parameters should be provided with a `using` clause.
| This code can be rewritten automatically under -rewrite -source 3.7-migration.
| To disable the warning, please use the following option:
| To disable the warning, please use the following option:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's nice to find a like-minded dev. By coincidence, today I:
image

Copy link
Contributor Author

@tgodzik tgodzik May 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My editor removes the space and I couldn't be bothered to find the setting responsible for it :S

| "-Wconf:msg=Implicit parameters should be provided with a `using` clause:s"
2 changes: 1 addition & 1 deletion tests/warn/i22440.check
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
| ^
| Implicit parameters should be provided with a `using` clause.
| This code can be rewritten automatically under -rewrite -source 3.7-migration.
| To disable the warning, please use the following option:
| To disable the warning, please use the following option:
| "-Wconf:msg=Implicit parameters should be provided with a `using` clause:s"
4 changes: 2 additions & 2 deletions tests/warn/i22731.check
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
| ^^^^^^^^
| Implicit parameters should be provided with a `using` clause.
| This code can be rewritten automatically under -rewrite -source 3.7-migration.
| To disable the warning, please use the following option:
| To disable the warning, please use the following option:
| "-Wconf:msg=Implicit parameters should be provided with a `using` clause:s"
-- Warning: tests/warn/i22731.scala:7:6 --------------------------------------------------------------------------------
7 | foo { () => 43 } // warn
| ^^^^^^^^^^^^
| Implicit parameters should be provided with a `using` clause.
| To disable the warning, please use the following option:
| To disable the warning, please use the following option:
| "-Wconf:msg=Implicit parameters should be provided with a `using` clause:s"
Loading