Skip to content

bugfix: Fix adjust type when already exists #23455

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

Merged
merged 2 commits into from
Jun 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,7 @@ final class InferredTypeProvider(
typeNameEdit ::: imports

rhs match
case t: Tree[?]
if t.typeOpt.isErroneous && retryType && !tpt.sourcePos.span.isZeroExtent =>
case t: Tree[?] if !tpt.sourcePos.span.isZeroExtent =>
inferredTypeEdits(
Some(
AdjustTypeOpts(
Expand Down Expand Up @@ -223,8 +222,7 @@ final class InferredTypeProvider(
while i >= 0 && sourceText(i) != ':' do i -= 1
i
rhs match
case t: Tree[?]
if t.typeOpt.isErroneous && retryType && !tpt.sourcePos.span.isZeroExtent =>
case t: Tree[?] if !tpt.sourcePos.span.isZeroExtent =>
inferredTypeEdits(
Some(
AdjustTypeOpts(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1055,6 +1055,70 @@ class InsertInferredTypeSuite extends BaseCodeActionSuite:
|""".stripMargin
)

@Test def `Adjust type for val` =
checkEdit(
"""|object A{
| val <<alpha>>:String = 123
|}""".stripMargin,

"""|object A{
| val alpha: Int = 123
|}""".stripMargin,
)

@Test def `Adjust type for val2` =
checkEdit(
"""|object A{
| val <<alpha>>:Int = 123
|}""".stripMargin,
"""|object A{
| val alpha: Int = 123
|}""".stripMargin,
)

@Test def `Adjust type for val3` =
checkEdit(
"""|object A{
| val <<alpha>>: Int = 123
|}""".stripMargin,
"""|object A{
| val alpha: Int = 123
|}""".stripMargin,
)

@Test def `Adjust type for def` =
checkEdit(
"""|object A{
| def <<alpha>>:String = 123
|}""".stripMargin,

"""|object A{
| def alpha: Int = 123
|}""".stripMargin,
)

@Test def `Adjust type for def2` =
checkEdit(
"""|object A{
| def <<alpha>>:Int = 123
|}""".stripMargin,
"""|object A{
| def alpha: Int = 123
|}""".stripMargin,
)


@Test def `Adjust type for def3` =
checkEdit(
"""|object A{
| def <<alpha>>: Int = 123
|}""".stripMargin,
"""|object A{
| def alpha: Int = 123
|}""".stripMargin,
)


def checkEdit(
original: String,
expected: String
Expand Down
Loading