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

Add noUniversalEquality option to DisableSyntax #877

Merged
merged 4 commits into from
Sep 21, 2018
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
Original file line number Diff line number Diff line change
Expand Up @@ -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(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
Expand All @@ -211,11 +217,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("==", config.noUniversalEqualityMessage, t.pos)

}

object DisableSyntax {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,15 @@ 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(
"Reporter message for noUniversalEquality"
)
noUniversalEqualityMessage: String =
"Universal equality should be avoided since it's not typesafe",
Copy link
Contributor

Choose a reason for hiding this comment

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

We may want to refine this message down the road, but it's not blocking

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'm open to suggestions :)

Copy link
Contributor

Choose a reason for hiding this comment

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

== is unsafe since it allows comparing two unrelated types

@Description(
"Report error if the text contents of a source file matches a given regex.")
@ExampleValue(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ DisableSyntax.noDefaultArgs = true
DisableSyntax.noValInAbstract = true
DisableSyntax.noImplicitObject = true
DisableSyntax.noImplicitConversion = true
DisableSyntax.noUniversalEquality = true
DisableSyntax.noUniversalEqualityMessage =
"== is not typesafe, use === from cats.Eq instead"
*/
package test

Expand Down Expand Up @@ -85,4 +88,15 @@ 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.==
^^
== is not typesafe, use === from cats.Eq instead
*/

1.==(2) /* assert: DisableSyntax.==
^^
== is not typesafe, use === from cats.Eq instead
*/

}