Skip to content

Commit

Permalink
Warn about unindented args
Browse files Browse the repository at this point in the history
  • Loading branch information
som-snytt committed Feb 13, 2025
1 parent 959e153 commit b4a4319
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 9 deletions.
10 changes: 8 additions & 2 deletions compiler/src/dotty/tools/dotc/parsing/Scanners.scala
Original file line number Diff line number Diff line change
Expand Up @@ -636,8 +636,14 @@ object Scanners {
if skipping then
if r.enclosing.isClosedByUndentAt(nextWidth) then
insert(OUTDENT, offset)
else if r.isInstanceOf[InBraces] && !closingRegionTokens.contains(token) then
report.warning("Line is indented too far to the left, or a `}` is missing", sourcePos())
else
val checkable = r match
case _: InBraces => true
case InParens(LPAREN, _) => true
case _ => false
if checkable && !closingRegionTokens.contains(token) then
report.warning(s"Line is indented too far to the left, or a ${showToken(r.closedBy)} is missing",
sourcePos())
else if lastWidth < nextWidth
|| lastWidth == nextWidth && (lastToken == MATCH || lastToken == CATCH) && token == CASE then
if canStartIndentTokens.contains(lastToken) then
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1026,7 +1026,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
addParamssText(
addParamssText(keywordStr("extension "), leadingParamss)
~~ (defKeyword ~~ valDefText(nameIdText(tree))).close,
trailingParamss)
trailingParamss)
else
addParamssText(defKeyword ~~ valDefText(nameIdText(tree)), tree.paramss)

Expand Down
6 changes: 2 additions & 4 deletions tests/neg/syntax-error-recovery.check
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,5 @@
| longer explanation available when compiling with `-explain`
-- Warning: tests/neg/syntax-error-recovery.scala:61:2 -----------------------------------------------------------------
61 | println(bam)
| ^^^^^^^
| Alphanumeric method println is not declared infix; it should not be used as infix operator.
| Instead, use method syntax .println(...) or backticked identifier `println`.
| The latter can be rewritten automatically under -rewrite -source 3.4-migration.
| ^
| Line is indented too far to the left, or a ')' is missing
9 changes: 7 additions & 2 deletions tests/pos/i22527.scala
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@

//> using options -Werror

import annotation.*

def f: Unit =
identity(
identity:
Expand Down Expand Up @@ -34,6 +38,7 @@ def orElse(x: Int): Unit =
else
false, "fail")

@nowarn("msg=Unit")
def onlyIf(x: Int): Unit =
callme(
if (x > 0)
Expand All @@ -57,8 +62,8 @@ def k(xs: List[Int], y: Int, z: Int) =
x
+ y
+ z,
y,
z,
y,
z,
)
)

Expand Down
22 changes: 22 additions & 0 deletions tests/warn/i22527.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
-- Warning: tests/warn/i22527.scala:60:10 ------------------------------------------------------------------------------
60 | y, // warn
| ^
| Line is indented too far to the left, or a ')' is missing
-- Warning: tests/warn/i22527.scala:61:10 ------------------------------------------------------------------------------
61 | z, // warn
| ^
| Line is indented too far to the left, or a ')' is missing
-- Warning: tests/warn/i22527.scala:72:2 -------------------------------------------------------------------------------
72 | 42 // warn
| ^
| Line is indented too far to the left, or a ')' is missing
-- Warning: tests/warn/i22527.scala:79:0 -------------------------------------------------------------------------------
79 |k: // warn
|^
|Line is indented too far to the left, or a ')' is missing
-- [E190] Potential Issue Warning: tests/warn/i22527.scala:40:6 --------------------------------------------------------
40 | true, "fail") // warn value discard
| ^^^^
| Discarded non-Unit value of type Boolean. Add `: Unit` to discard silently.
|
| longer explanation available when compiling with `-explain`
81 changes: 81 additions & 0 deletions tests/warn/i22527.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@

def f: Unit =
identity(
identity:
class X extends AnyRef, Serializable
42
)

def test: Unit =
assert(
identity:
true,
"ok"
)

def toss: Unit =
assert(
throw
null,
"ok"
)
def raise: Unit =
assert(
throw
null, "ok" // ok now
)

def callme[A](x: => A, msg: String) = try x.toString catch case t: RuntimeException => msg

def orElse(x: Int): Unit =
callme(
if x > 0 then
true
else
false, "fail")

def onlyIf(x: Int): Unit =
callme(
if (x > 0)
true, "fail") // warn value discard

def h(xs: List[Int]) =
xs.foldLeft(0)
(
(acc, x) =>
acc
+ x,
)

def sum(x: Int, y: Int, z: Int) = x+y+z

def k(xs: List[Int], y: Int, z: Int) =
xs.foldLeft(0)
(
(acc, x) =>
sum(
x
+ y
+ z,
y, // warn
z, // warn
)
)

object `arrow eol`:
def f(g: Int => Int, i: Int): Int = g(i)
def g(map: Int => Int): Int => Int = map
def k(i: => Int) = i
def test =
f(
g(_ + 1),
42 // warn
)
def test2 =
f(
g:
x =>
x + 1,
k: // warn
42
)

0 comments on commit b4a4319

Please sign in to comment.