Skip to content
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

Make NoAutoTupling fixes the insertion of unit #1452

Merged
merged 1 commit into from
Jul 16, 2021
Merged
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
14 changes: 5 additions & 9 deletions docs/rules/NoAutoTupling.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,23 @@ id: NoAutoTupling
title: NoAutoTupling
---

> ⚠️ This rule does not work with Scala 2.13 since `-Yno-adapter-args` has been
> removed.

Adds explicit tuples around argument lists where auto-tupling is occurring.

To use this rule:

- enable `-Ywarn-adapted-args` (note, `-Yno-adapted-args` will fail compilation,
which prevents scalafix from running)
- disable `-Xfatal-warnings`. Unfortunately, the Scala compiler does not support
finer grained control over the severity level per message kind. See
[scalameta/scalameta#924](https://github.com/scalameta/scalameta/issues/924)
Comment on lines -16 to -18
Copy link
Collaborator

Choose a reason for hiding this comment

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

Did you remove that following scalacenter/sbt-scalafix#196? It's specific to sbt-scalafix, but I guess it's not worth mentioning "just" for the other clients, so I am inclined to say that we should ignore mentions of -Xfatal-warnings considering sbt-scalafix is the main client.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

yes and I forgot about the other clients. I think the other client should mention it (or are already menting it)

for a possible workaround in the near future.
- enable `-Ywarn-adapted-args` for Scala 2.11 and 2.12 (note, `-Yno-adapted-args` will fail compilation,
which prevents scalafix from running). For Scala 2.13, use instead `-Xlint:adapted-args`.
- enable also `-deprecation` to get warnings on insertions of `Unit`.

```scala
// before
def someMethod(t: (Int, String)) = ...
someMethod(1, "something")
val c: Option[Unit] = Some()
// after
def someMethod(t: (Int, String)) = ...
someMethod((1, "something"))
val c: Option[Unit] = Some(())
```

Auto-tupling is a feature that can lead to unexpected results, making code to
Expand Down
4 changes: 2 additions & 2 deletions project/ScalafixBuild.scala
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ object ScalafixBuild extends AutoPlugin with GhpagesKeys {
}
val warnAdaptedArgs = Def.setting {
if (isScala3.value) Nil
else if (isScala213.value) Seq("-Xlint:adapted-args")
else Seq("-Ywarn-adapted-args")
else if (isScala213.value) Seq("-Xlint:adapted-args", "-deprecation")
else Seq("-Ywarn-adapted-args", "-deprecation")
}
lazy val scaladocOptions = Seq(
"-groups",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ class NoAutoTupling extends SemanticRule("NoAutoTupling") {
case message
if message.message.startsWith(
"Adaptation of argument list by inserting ()"
) || message.message.startsWith(
"adaptation of an empty argument list by inserting ()"
) =>
message.position
}.toSet
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/*
rules = NoAutoTupling
*/
package test
package test.noAutoTupling

class NoAutoTupling2 {
class NoAutoTupling {
def a(x: (Int, Boolean)) = x
a(2, true)

Expand Down Expand Up @@ -48,7 +48,4 @@ class NoAutoTupling2 {
Foo(42, "foo", ('z', true))
Foo.apply(42, "foo", ('z', true))
}

//todo: Fix Adaptation of argument list by inserting ()
val c: Option[Unit] = Some()
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
rules = NoAutoTupling
*/
package test
package test.noAutoTupling

class NoUnitInsertion {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package test
package test.noAutoTupling

class NoAutoTupling2 {
class NoAutoTupling {
def a(x: (Int, Boolean)) = x
a((2, true))

Expand Down Expand Up @@ -45,8 +45,5 @@ class NoAutoTupling2 {
Foo(42, "foo", ('z', true))
Foo.apply(42, "foo", ('z', true))
}

//todo: Fix Adaptation of argument list by inserting ()
val c: Option[Unit] = Some()
}

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package test
package test.noAutoTupling

class NoAutoTupling2 {
class NoAutoTupling {
def a(x: (Int, Boolean)) = x
a((2, true))

Expand Down Expand Up @@ -45,8 +45,5 @@ class NoAutoTupling2 {
Foo(42, "foo", ('z', true))
Foo.apply(42, "foo", ('z', true))
}

//todo: Fix Adaptation of argument list by inserting ()
val c: Option[Unit] = Some()
}

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package test
package test.noAutoTupling

class NoAutoTupling2 {
class NoAutoTupling {
def a(x: (Int, Boolean)) = x
a((2, true))

Expand Down Expand Up @@ -45,8 +45,5 @@ class NoAutoTupling2 {
Foo(42, "foo", ('z', true))
Foo.apply(42, "foo", ('z', true))
}

//todo: Fix Adaptation of argument list by inserting ()
val c: Option[Unit] = Some()
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package test.noAutoTupling

class NoUnitInsertion {
Copy link
Collaborator

Choose a reason for hiding this comment

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

so if I understand well:

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

  • I didn't know why this test wasn't running. I deleted it by mistake!
  • While grouping the tests together for each rule, I remembered that a small thing was missing to make this rule working. Maybe I will go throw my previous Todo and see what I can fix.


val x: Option[Unit] = Option(())

def a(u: Unit): Unit = u
a(())

def b(x: Int)(u: Unit): Unit = (x, u)
b(2)(())

val c: Unit => Unit =
u => u
c(())

case class Foo(u: Unit)
Foo(())
Foo.apply(())

case class Bar(i: Int)(u: Unit)
Bar.apply(2)(())

}