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
Discourage multi-argument infix syntax: lint applications (x op (a, b)
), also lint operator-name definitions
#8951
Merged
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6cf73d0
Lint multiarg infix operand
som-snytt cd6d4a1
Reduce multiarg infix applications
som-snytt 25bbbf4
Don't warn about implicit multiargs
som-snytt 54f5e75
Infix ops are not local defs
som-snytt 559f892
Don't warn multiarg infix if deprecated or unused
som-snytt 841b880
Avoid multiarg infix warning on Subtractable
som-snytt File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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
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,35 @@ | ||
sd503.scala:18: error: not enough arguments for method x_=: (i: Int, j: Int): Unit. | ||
Unspecified value parameter j. | ||
def f4() = c.x = (42, 27) // missing arg | ||
^ | ||
sd503.scala:23: error: type mismatch; | ||
found : (Int, Int) | ||
required: Int | ||
def f7() = d.x = (42, 27) // type mismatch (same as doti) | ||
^ | ||
sd503.scala:9: warning: multiarg infix syntax looks like a tuple and will be deprecated | ||
def % (i: Int, j: Int) = i + j // operator, warn | ||
^ | ||
sd503.scala:29: warning: multiarg infix syntax looks like a tuple and will be deprecated | ||
def x_=(i: Int, j: Int): Unit = value = i + j // multiarg, warn | ||
^ | ||
sd503.scala:34: warning: multiarg infix syntax looks like a tuple and will be deprecated | ||
def x_=(i: Int, j: Int = 1): Unit = devalue = i + j // multiarg, warn | ||
^ | ||
sd503.scala:47: warning: multiarg infix syntax looks like a tuple and will be deprecated | ||
def +=(x: A, y: A, zs: A*): this.type = addAll(x +: y +: zs) // very multiarg, warn | ||
^ | ||
sd503.scala:13: warning: multiarg infix syntax looks like a tuple and will be deprecated | ||
def f1(t: T) = t m (1, 2) // multiarg, warn | ||
^ | ||
sd503.scala:15: warning: multiarg infix syntax looks like a tuple and will be deprecated | ||
def f3(t: T) = t % (1, 2) // multiarg, warn | ||
^ | ||
sd503.scala:19: warning: multiarg infix syntax looks like a tuple and will be deprecated | ||
def f5() = c x_= (42, 27) // multiarg, warn | ||
^ | ||
sd503.scala:52: warning: multiarg infix syntax looks like a tuple and will be deprecated | ||
def f[A](as: Embiggen[A], x: A, y: A, z: A): as.type = as += (x, y, z) // very multiarg, warn | ||
^ | ||
8 warnings | ||
2 errors |
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,53 @@ | ||
// scalac: -Xlint:multiarg-infix | ||
// | ||
// lint multiarg infix syntax, e.g., vs += (1, 2, 3) | ||
// Since infix is encouraged by symbolic operator names, discourage defining def += (x: A, y: A, zs: A*) | ||
|
||
trait T { | ||
def m(i: Int, j: Int) = i + j | ||
|
||
def % (i: Int, j: Int) = i + j // operator, warn | ||
|
||
|
||
def f0(t: T) = t.m(1, 2) // ok | ||
def f1(t: T) = t m (1, 2) // multiarg, warn | ||
def f2(t: T) = t.%(1, 2) // ok | ||
def f3(t: T) = t % (1, 2) // multiarg, warn | ||
|
||
def c = new C | ||
def f4() = c.x = (42, 27) // missing arg | ||
def f5() = c x_= (42, 27) // multiarg, warn | ||
|
||
def d = new D | ||
def f6() = d.x = 42 // ok! | ||
def f7() = d.x = (42, 27) // type mismatch (same as doti) | ||
} | ||
|
||
class C { | ||
private var value: Int = _ | ||
def x: Int = value | ||
def x_=(i: Int, j: Int): Unit = value = i + j // multiarg, warn | ||
} | ||
class D { | ||
private var devalue: Int = _ // d.value | ||
def x: Int = devalue | ||
def x_=(i: Int, j: Int = 1): Unit = devalue = i + j // multiarg, warn | ||
} | ||
|
||
// If the application is adapted such that what looks like a tuple is taken as a tuple, | ||
// then don't warn; eventually this will be normal behavior. | ||
trait OK { | ||
def f(p: (Int, Int)) = p == (42, 17) // nowarn! | ||
def g(ps: Embiggen[(Int, Int)]) = ps :+ (42, 17) // nowarn! | ||
} | ||
|
||
// Growable is the paradigmatic example | ||
trait Embiggen[A] { | ||
def addAll(as: Seq[A]): this.type | ||
def +=(x: A, y: A, zs: A*): this.type = addAll(x +: y +: zs) // very multiarg, warn | ||
def :+(a: A): Embiggen[A] | ||
} | ||
|
||
trait NotOK { | ||
def f[A](as: Embiggen[A], x: A, y: A, z: A): as.type = as += (x, y, z) // very multiarg, warn | ||
} |
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 |
---|---|---|
@@ -1,2 +1,17 @@ | ||
infix.scala:6: warning: multiarg infix syntax looks like a tuple and will be deprecated | ||
val xs = new op(null, 0, 0) op (1, 1) op (2, 2) | ||
^ | ||
infix.scala:6: warning: multiarg infix syntax looks like a tuple and will be deprecated | ||
val xs = new op(null, 0, 0) op (1, 1) op (2, 2) | ||
^ | ||
infix.scala:9: warning: multiarg infix syntax looks like a tuple and will be deprecated | ||
case null op (0, 0) op (1, 1) op (2, 2) => Console.println("OK") | ||
^ | ||
infix.scala:9: warning: multiarg infix syntax looks like a tuple and will be deprecated | ||
case null op (0, 0) op (1, 1) op (2, 2) => Console.println("OK") | ||
^ | ||
infix.scala:9: warning: multiarg infix syntax looks like a tuple and will be deprecated | ||
case null op (0, 0) op (1, 1) op (2, 2) => Console.println("OK") | ||
^ | ||
op(op(op(null,0,0),1,1),2,2) | ||
OK |
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,3 @@ | ||
t5565.scala:10: warning: multiarg infix syntax looks like a tuple and will be deprecated | ||
assert(math.abs(-4.0) ~== (4.0, 0.001)) | ||
^ |
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
Oops, something went wrong.
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.
The inner parens are extraneous.