-
Notifications
You must be signed in to change notification settings - Fork 185
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
Implement NoInfer linter. #333
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
scalafix-core/shared/src/main/scala/org/langmeta/internal/ScalafixLangmetaHacks.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package org.langmeta.internal | ||
|
||
import scala.compat.Platform.EOL | ||
import org.langmeta._ | ||
|
||
object ScalafixLangmetaHacks { | ||
// Workaround for https://github.com/scalameta/scalameta/issues/1115 | ||
def formatMessage(pos: Position, severity: String, message: String): String = { | ||
if (pos != Position.None) { | ||
val input = pos.input | ||
val header = | ||
s"${input.syntax}:${pos.startLine + 1}: $severity: $message" | ||
val line = { | ||
val start = input.lineToOffset(pos.startLine) | ||
val notEof = start < input.chars.length | ||
val end = if (notEof) input.lineToOffset(pos.startLine + 1) else start | ||
val count = end - start | ||
val eolOffset = | ||
if (input.chars.lift(start + count - 1).contains('\n')) -1 else 0 | ||
new String(input.chars, start, count + eolOffset) | ||
} | ||
var caret = " " * pos.startColumn + "^" | ||
header + EOL + line + EOL + caret | ||
} else { | ||
s"$severity: $message" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
scalafix-core/shared/src/main/scala/scalafix/internal/rule/NoInfer.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package scalafix.internal.rule | ||
|
||
import scala.meta._ | ||
import scalafix.lint.LintCategory | ||
import scalafix.lint.LintMessage | ||
import scalafix.rule.RuleCtx | ||
import scalafix.rule.SemanticRule | ||
import scalafix.util.SemanticdbIndex | ||
import scalafix.util.SymbolMatcher | ||
|
||
case class NoInfer(index: SemanticdbIndex) | ||
extends SemanticRule(index, "NoInfer") | ||
with Product { | ||
private val badSymbol = SymbolMatcher.exact(NoInfer.badSymbols: _*) | ||
val error: LintCategory = | ||
LintCategory.error( | ||
"""The Scala compiler sometimes infers a too generic type such as Any. | ||
|If this is intended behavior, then the type should be explicitly type | ||
|annotated in the source.""".stripMargin | ||
) | ||
override def check(ctx: RuleCtx): Seq[LintMessage] = | ||
ctx.index.synthetics.flatMap { | ||
case Synthetic(pos, _, names) => | ||
names.collect { | ||
case ResolvedName(_, badSymbol(Symbol.Global(_, signature)), _) => | ||
val categoryId = signature.name.toLowerCase() | ||
error | ||
.copy(id = categoryId) | ||
.at(s"Inferred ${signature.name}", pos) | ||
} | ||
} | ||
} | ||
|
||
case object NoInfer { | ||
lazy val badSymbols: List[Symbol] = List( | ||
Symbol("_root_.java.io.Serializable#"), | ||
Symbol("_root_.scala.Any#"), | ||
Symbol("_root_.scala.AnyVal#"), | ||
Symbol("_root_.scala.AnyVal#"), | ||
Symbol("_root_.scala.Product#") | ||
) | ||
|
||
def badSymbolNames: List[String] = badSymbols.collect { | ||
case Symbol.Global(_, signature) => signature.name | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/* | ||
rule = NoInfer | ||
*/ | ||
package test | ||
|
||
case object NoInfer { | ||
val x = List(1, "")// assert: NoInfer.any | ||
x.map(x => x -> x)// assert: NoInfer.any | ||
List[Any](1, "") // OK, not reported message | ||
List[Any](1, "").map(identity[Any])/*(canBuildFrom[Any])*/// assert: NoInfer.any | ||
(null: Any) | ||
null match { | ||
case _: Any => | ||
} | ||
case class A() | ||
List(NoInfer, A())// assert: NoInfer.product | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
how does the
// assert
comment work?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can assert which linter message is reported at which specfic lines https://github.com/scalacenter/scalafix/blob/master/scalafix-tests/input/src/main/scala/test/NoInfer.scala#L7
The test fails if
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice! This is only for testkit right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, this is only testkit. Escape hatches for users are yet unimplemented #241