Skip to content

Commit b4a4319

Browse files
committed
Warn about unindented args
1 parent 959e153 commit b4a4319

File tree

6 files changed

+121
-9
lines changed

6 files changed

+121
-9
lines changed

compiler/src/dotty/tools/dotc/parsing/Scanners.scala

+8-2
Original file line numberDiff line numberDiff line change
@@ -636,8 +636,14 @@ object Scanners {
636636
if skipping then
637637
if r.enclosing.isClosedByUndentAt(nextWidth) then
638638
insert(OUTDENT, offset)
639-
else if r.isInstanceOf[InBraces] && !closingRegionTokens.contains(token) then
640-
report.warning("Line is indented too far to the left, or a `}` is missing", sourcePos())
639+
else
640+
val checkable = r match
641+
case _: InBraces => true
642+
case InParens(LPAREN, _) => true
643+
case _ => false
644+
if checkable && !closingRegionTokens.contains(token) then
645+
report.warning(s"Line is indented too far to the left, or a ${showToken(r.closedBy)} is missing",
646+
sourcePos())
641647
else if lastWidth < nextWidth
642648
|| lastWidth == nextWidth && (lastToken == MATCH || lastToken == CATCH) && token == CASE then
643649
if canStartIndentTokens.contains(lastToken) then

compiler/src/dotty/tools/dotc/printing/RefinedPrinter.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -1026,7 +1026,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
10261026
addParamssText(
10271027
addParamssText(keywordStr("extension "), leadingParamss)
10281028
~~ (defKeyword ~~ valDefText(nameIdText(tree))).close,
1029-
trailingParamss)
1029+
trailingParamss)
10301030
else
10311031
addParamssText(defKeyword ~~ valDefText(nameIdText(tree)), tree.paramss)
10321032

tests/neg/syntax-error-recovery.check

+2-4
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,5 @@
9696
| longer explanation available when compiling with `-explain`
9797
-- Warning: tests/neg/syntax-error-recovery.scala:61:2 -----------------------------------------------------------------
9898
61 | println(bam)
99-
| ^^^^^^^
100-
| Alphanumeric method println is not declared infix; it should not be used as infix operator.
101-
| Instead, use method syntax .println(...) or backticked identifier `println`.
102-
| The latter can be rewritten automatically under -rewrite -source 3.4-migration.
99+
| ^
100+
| Line is indented too far to the left, or a ')' is missing

tests/pos/i22527.scala

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11

2+
//> using options -Werror
3+
4+
import annotation.*
5+
26
def f: Unit =
37
identity(
48
identity:
@@ -34,6 +38,7 @@ def orElse(x: Int): Unit =
3438
else
3539
false, "fail")
3640

41+
@nowarn("msg=Unit")
3742
def onlyIf(x: Int): Unit =
3843
callme(
3944
if (x > 0)
@@ -57,8 +62,8 @@ def k(xs: List[Int], y: Int, z: Int) =
5762
x
5863
+ y
5964
+ z,
60-
y,
61-
z,
65+
y,
66+
z,
6267
)
6368
)
6469

tests/warn/i22527.check

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
-- Warning: tests/warn/i22527.scala:60:10 ------------------------------------------------------------------------------
2+
60 | y, // warn
3+
| ^
4+
| Line is indented too far to the left, or a ')' is missing
5+
-- Warning: tests/warn/i22527.scala:61:10 ------------------------------------------------------------------------------
6+
61 | z, // warn
7+
| ^
8+
| Line is indented too far to the left, or a ')' is missing
9+
-- Warning: tests/warn/i22527.scala:72:2 -------------------------------------------------------------------------------
10+
72 | 42 // warn
11+
| ^
12+
| Line is indented too far to the left, or a ')' is missing
13+
-- Warning: tests/warn/i22527.scala:79:0 -------------------------------------------------------------------------------
14+
79 |k: // warn
15+
|^
16+
|Line is indented too far to the left, or a ')' is missing
17+
-- [E190] Potential Issue Warning: tests/warn/i22527.scala:40:6 --------------------------------------------------------
18+
40 | true, "fail") // warn value discard
19+
| ^^^^
20+
| Discarded non-Unit value of type Boolean. Add `: Unit` to discard silently.
21+
|
22+
| longer explanation available when compiling with `-explain`

tests/warn/i22527.scala

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
2+
def f: Unit =
3+
identity(
4+
identity:
5+
class X extends AnyRef, Serializable
6+
42
7+
)
8+
9+
def test: Unit =
10+
assert(
11+
identity:
12+
true,
13+
"ok"
14+
)
15+
16+
def toss: Unit =
17+
assert(
18+
throw
19+
null,
20+
"ok"
21+
)
22+
def raise: Unit =
23+
assert(
24+
throw
25+
null, "ok" // ok now
26+
)
27+
28+
def callme[A](x: => A, msg: String) = try x.toString catch case t: RuntimeException => msg
29+
30+
def orElse(x: Int): Unit =
31+
callme(
32+
if x > 0 then
33+
true
34+
else
35+
false, "fail")
36+
37+
def onlyIf(x: Int): Unit =
38+
callme(
39+
if (x > 0)
40+
true, "fail") // warn value discard
41+
42+
def h(xs: List[Int]) =
43+
xs.foldLeft(0)
44+
(
45+
(acc, x) =>
46+
acc
47+
+ x,
48+
)
49+
50+
def sum(x: Int, y: Int, z: Int) = x+y+z
51+
52+
def k(xs: List[Int], y: Int, z: Int) =
53+
xs.foldLeft(0)
54+
(
55+
(acc, x) =>
56+
sum(
57+
x
58+
+ y
59+
+ z,
60+
y, // warn
61+
z, // warn
62+
)
63+
)
64+
65+
object `arrow eol`:
66+
def f(g: Int => Int, i: Int): Int = g(i)
67+
def g(map: Int => Int): Int => Int = map
68+
def k(i: => Int) = i
69+
def test =
70+
f(
71+
g(_ + 1),
72+
42 // warn
73+
)
74+
def test2 =
75+
f(
76+
g:
77+
x =>
78+
x + 1,
79+
k: // warn
80+
42
81+
)

0 commit comments

Comments
 (0)