From 619817bfac1cefe56962a651be5838fff66a90b9 Mon Sep 17 00:00:00 2001 From: Gabriele Petronella Date: Thu, 20 Sep 2018 19:00:33 +0200 Subject: [PATCH 1/4] Add noUniversalEquality option to DisableSyntax --- .../main/scala/scalafix/internal/rule/DisableSyntax.scala | 6 ++++++ .../scala/scalafix/internal/rule/DisableSyntaxConfig.scala | 4 ++++ .../input/src/main/scala/test/DisableSyntaxMoreRules.scala | 6 ++++++ 3 files changed, 16 insertions(+) diff --git a/scalafix-rules/src/main/scala/scalafix/internal/rule/DisableSyntax.scala b/scalafix-rules/src/main/scala/scalafix/internal/rule/DisableSyntax.scala index 96a5dae8a..e2cb863a9 100644 --- a/scalafix-rules/src/main/scala/scalafix/internal/rule/DisableSyntax.scala +++ b/scalafix-rules/src/main/scala/scalafix/internal/rule/DisableSyntax.scala @@ -196,6 +196,12 @@ final class DisableSyntax(config: DisableSyntaxConfig) "Default args makes it hard to use methods as functions.", m.pos) } + case Term.ApplyInfix(_, t @ Term.Name("=="), _, _) if config.noUniversalEquality => + Seq(Diagnostic( + "noUniversalEquality", + "== (universal equality) is disabled", + t.pos + )) } val FinalizeMatcher = DisableSyntax.FinalizeMatcher("noFinalize") doc.tree.collect(DefaultMatcher.orElse(FinalizeMatcher)).flatten diff --git a/scalafix-rules/src/main/scala/scalafix/internal/rule/DisableSyntaxConfig.scala b/scalafix-rules/src/main/scala/scalafix/internal/rule/DisableSyntaxConfig.scala index 2c342218f..875153198 100644 --- a/scalafix-rules/src/main/scala/scalafix/internal/rule/DisableSyntaxConfig.scala +++ b/scalafix-rules/src/main/scala/scalafix/internal/rule/DisableSyntaxConfig.scala @@ -64,6 +64,10 @@ case class DisableSyntaxConfig( @Description( "Report error when pattern matching in val assignment with non-tuple patterns.") noValPatterns: Boolean = false, + @Description( + "Report error on `==` (universal equality)" + ) + noUniversalEquality: Boolean = false, @Description( "Report error if the text contents of a source file matches a given regex.") @ExampleValue( diff --git a/scalafix-tests/input/src/main/scala/test/DisableSyntaxMoreRules.scala b/scalafix-tests/input/src/main/scala/test/DisableSyntaxMoreRules.scala index 73af1f223..ecd4d7f01 100644 --- a/scalafix-tests/input/src/main/scala/test/DisableSyntaxMoreRules.scala +++ b/scalafix-tests/input/src/main/scala/test/DisableSyntaxMoreRules.scala @@ -6,6 +6,7 @@ DisableSyntax.noDefaultArgs = true DisableSyntax.noValInAbstract = true DisableSyntax.noImplicitObject = true DisableSyntax.noImplicitConversion = true +DisableSyntax.noUniversalEquality = true */ package test @@ -85,4 +86,9 @@ Default args makes it hard to use methods as functions. implicit def toString(a: Any): String = a.toString // assert: DisableSyntax.implicitConversion implicit def toImplicitString(implicit foo: Foo) = foo.toString // ok + + 1 == 2 /* assert: DisableSyntax.noUniversalEquality + ^^ + == (universal equality) is disabled + */ } From c594cf604c8d7abb22a64c24970d73a415e5d604 Mon Sep 17 00:00:00 2001 From: Gabriele Petronella Date: Fri, 21 Sep 2018 10:24:59 +0200 Subject: [PATCH 2/4] Address code review comments --- .../scalafix/internal/rule/DisableSyntax.scala | 13 ++++++++----- .../main/scala/test/DisableSyntaxMoreRules.scala | 8 +++++++- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/scalafix-rules/src/main/scala/scalafix/internal/rule/DisableSyntax.scala b/scalafix-rules/src/main/scala/scalafix/internal/rule/DisableSyntax.scala index e2cb863a9..2e7b6d8e5 100644 --- a/scalafix-rules/src/main/scala/scalafix/internal/rule/DisableSyntax.scala +++ b/scalafix-rules/src/main/scala/scalafix/internal/rule/DisableSyntax.scala @@ -197,11 +197,9 @@ final class DisableSyntax(config: DisableSyntaxConfig) m.pos) } case Term.ApplyInfix(_, t @ Term.Name("=="), _, _) if config.noUniversalEquality => - Seq(Diagnostic( - "noUniversalEquality", - "== (universal equality) is disabled", - t.pos - )) + Seq(noUniversalEqualityDiagnostic(t)) + case Term.Apply(Term.Select(_, t @ Term.Name("==")), _) if config.noUniversalEquality => + Seq(noUniversalEqualityDiagnostic(t)) } val FinalizeMatcher = DisableSyntax.FinalizeMatcher("noFinalize") doc.tree.collect(DefaultMatcher.orElse(FinalizeMatcher)).flatten @@ -217,11 +215,16 @@ final class DisableSyntax(config: DisableSyntaxConfig) LintCategory.error( id = "noFinalVal", explain = "Final vals cause problems with incremental compilation") + private val noValPatternCategory: LintCategory = LintCategory.error( id = "noValPatterns", explain = "Pattern matching in val assignment can result in match error, " + "use \"_ match { ... }\" with a fallback case instead.") + + private def noUniversalEqualityDiagnostic(t: Term.Name): Diagnostic = + Diagnostic("==", "== (universal equality) is disabled", t.pos) + } object DisableSyntax { diff --git a/scalafix-tests/input/src/main/scala/test/DisableSyntaxMoreRules.scala b/scalafix-tests/input/src/main/scala/test/DisableSyntaxMoreRules.scala index ecd4d7f01..a00352f0c 100644 --- a/scalafix-tests/input/src/main/scala/test/DisableSyntaxMoreRules.scala +++ b/scalafix-tests/input/src/main/scala/test/DisableSyntaxMoreRules.scala @@ -87,8 +87,14 @@ Default args makes it hard to use methods as functions. implicit def toString(a: Any): String = a.toString // assert: DisableSyntax.implicitConversion implicit def toImplicitString(implicit foo: Foo) = foo.toString // ok - 1 == 2 /* assert: DisableSyntax.noUniversalEquality + 1 == 2 /* assert: DisableSyntax.== ^^ == (universal equality) is disabled */ + + 1.==(2) /* assert: DisableSyntax.== + ^^ + == (universal equality) is disabled + */ + } From a46ce95dfc40e2216e19f4c9faf98797aac4743b Mon Sep 17 00:00:00 2001 From: Gabriele Petronella Date: Fri, 21 Sep 2018 10:30:52 +0200 Subject: [PATCH 3/4] Allow configuring the noUniversalEquality reporter message --- .../main/scala/scalafix/internal/rule/DisableSyntax.scala | 2 +- .../scala/scalafix/internal/rule/DisableSyntaxConfig.scala | 4 ++++ .../input/src/main/scala/test/DisableSyntaxMoreRules.scala | 6 ++++-- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/scalafix-rules/src/main/scala/scalafix/internal/rule/DisableSyntax.scala b/scalafix-rules/src/main/scala/scalafix/internal/rule/DisableSyntax.scala index 2e7b6d8e5..c389b60c8 100644 --- a/scalafix-rules/src/main/scala/scalafix/internal/rule/DisableSyntax.scala +++ b/scalafix-rules/src/main/scala/scalafix/internal/rule/DisableSyntax.scala @@ -223,7 +223,7 @@ final class DisableSyntax(config: DisableSyntaxConfig) "use \"_ match { ... }\" with a fallback case instead.") private def noUniversalEqualityDiagnostic(t: Term.Name): Diagnostic = - Diagnostic("==", "== (universal equality) is disabled", t.pos) + Diagnostic("==", config.noUniversalEqualityMessage, t.pos) } diff --git a/scalafix-rules/src/main/scala/scalafix/internal/rule/DisableSyntaxConfig.scala b/scalafix-rules/src/main/scala/scalafix/internal/rule/DisableSyntaxConfig.scala index 875153198..7e67432ee 100644 --- a/scalafix-rules/src/main/scala/scalafix/internal/rule/DisableSyntaxConfig.scala +++ b/scalafix-rules/src/main/scala/scalafix/internal/rule/DisableSyntaxConfig.scala @@ -68,6 +68,10 @@ case class DisableSyntaxConfig( "Report error on `==` (universal equality)" ) noUniversalEquality: Boolean = false, + @Description( + "Reporter message for noUniversalEquality" + ) + noUniversalEqualityMessage: String = "Universal equality should be avoided since it's not typesafe", @Description( "Report error if the text contents of a source file matches a given regex.") @ExampleValue( diff --git a/scalafix-tests/input/src/main/scala/test/DisableSyntaxMoreRules.scala b/scalafix-tests/input/src/main/scala/test/DisableSyntaxMoreRules.scala index a00352f0c..01d56d193 100644 --- a/scalafix-tests/input/src/main/scala/test/DisableSyntaxMoreRules.scala +++ b/scalafix-tests/input/src/main/scala/test/DisableSyntaxMoreRules.scala @@ -7,6 +7,8 @@ DisableSyntax.noValInAbstract = true DisableSyntax.noImplicitObject = true DisableSyntax.noImplicitConversion = true DisableSyntax.noUniversalEquality = true +DisableSyntax.noUniversalEqualityMessage = + "== is not typesafe, use === from cats.Eq instead" */ package test @@ -89,12 +91,12 @@ Default args makes it hard to use methods as functions. 1 == 2 /* assert: DisableSyntax.== ^^ - == (universal equality) is disabled + == is not typesafe, use === from cats.Eq instead */ 1.==(2) /* assert: DisableSyntax.== ^^ - == (universal equality) is disabled + == is not typesafe, use === from cats.Eq instead */ } From d03a23025ad2cd33ae618c904c93c82849bf41fe Mon Sep 17 00:00:00 2001 From: Gabriele Petronella Date: Fri, 21 Sep 2018 10:31:36 +0200 Subject: [PATCH 4/4] Scalafmt --- .../main/scala/scalafix/internal/rule/DisableSyntax.scala | 6 ++++-- .../scala/scalafix/internal/rule/DisableSyntaxConfig.scala | 3 ++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/scalafix-rules/src/main/scala/scalafix/internal/rule/DisableSyntax.scala b/scalafix-rules/src/main/scala/scalafix/internal/rule/DisableSyntax.scala index c389b60c8..f0f2a4552 100644 --- a/scalafix-rules/src/main/scala/scalafix/internal/rule/DisableSyntax.scala +++ b/scalafix-rules/src/main/scala/scalafix/internal/rule/DisableSyntax.scala @@ -196,9 +196,11 @@ final class DisableSyntax(config: DisableSyntaxConfig) "Default args makes it hard to use methods as functions.", m.pos) } - case Term.ApplyInfix(_, t @ Term.Name("=="), _, _) if config.noUniversalEquality => + case Term.ApplyInfix(_, t @ Term.Name("=="), _, _) + if config.noUniversalEquality => Seq(noUniversalEqualityDiagnostic(t)) - case Term.Apply(Term.Select(_, t @ Term.Name("==")), _) if config.noUniversalEquality => + case Term.Apply(Term.Select(_, t @ Term.Name("==")), _) + if config.noUniversalEquality => Seq(noUniversalEqualityDiagnostic(t)) } val FinalizeMatcher = DisableSyntax.FinalizeMatcher("noFinalize") diff --git a/scalafix-rules/src/main/scala/scalafix/internal/rule/DisableSyntaxConfig.scala b/scalafix-rules/src/main/scala/scalafix/internal/rule/DisableSyntaxConfig.scala index 7e67432ee..7e139c2af 100644 --- a/scalafix-rules/src/main/scala/scalafix/internal/rule/DisableSyntaxConfig.scala +++ b/scalafix-rules/src/main/scala/scalafix/internal/rule/DisableSyntaxConfig.scala @@ -71,7 +71,8 @@ case class DisableSyntaxConfig( @Description( "Reporter message for noUniversalEquality" ) - noUniversalEqualityMessage: String = "Universal equality should be avoided since it's not typesafe", + noUniversalEqualityMessage: String = + "Universal equality should be avoided since it's not typesafe", @Description( "Report error if the text contents of a source file matches a given regex.") @ExampleValue(