-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Issue warnings if braces are missing #7270
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
Conversation
In a brace-delimited region, if there would be an "unindent" between one line and the next, a warning is now issued that states: Line is indented too far to the left or a `}' is missing This is very helpful in finding missing closing braces. The tests are fixed to avoid the warning except for triple-quoted-expr.scala. The latter has a """ at the start of a line.
Also, fix two more tests
Under -noindent, warn id code that follows an empty template is indented more deeply than the template header. Inserting braces automatically would change meaning in this case. Example: ```scala trait A case class B() extends A // error: Line is indented too far to the right case object C extends A // error: Line is indented too far to the right ```
c4985b5
to
46e1afe
Compare
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.
LGTM
I'm thinking if we could strengthen the rules for infix operators. Currently, the following program runs with no problem:
@main def Test = {
val a = 6
var b = 0
if (a > 5) then
b = a
- 5
assert(b == 1)
}
If would be good to check that indentation of infix operator should be bigger than that of currentRegion.outer
?
val nextIndentWidth = in.indentWidth(in.next.offset) | ||
if r.indentWidth < nextIndentWidth then | ||
warning(i"Line is indented too far to the right, or a `{' is missing", in.next.offset) | ||
case _ => |
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 check does not handle code at top-level:
trait A
case class B() extends A
case object C extends A
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.
OK, I'll add a fix to the follow-up PR.
Yes, maybe. But I am not sure the compiler has to do it. Currently the compiler only enforces those indentation rules that make sure that braces around well-indented code are optional. To do more, |
Issue warnings if brace structure does not correspond to indentation structure.
There are three cases.
Case 1: In a brace-delimited region, if there would be an
<outdent>
betweenone line and the next, a warning is now issued that states:
This is very helpful for finding missing closing braces. It prevents errors like:
Case 2: If significant indentation is turned off (i.e. under -noindent) and we are at the start
of an indented sub-part (i.e. an
<indent>
would have been inserted under significant indentation),and the indented part ends in a newline, check that the next statement starts at an indentation width less than the sub-part. This prevents errors like
Case 3: Under -noindent, warn if code that follows an empty template is
indented more deeply than the template header. Inserting braces
automatically would change meaning in this case.
Example: