4

This morning, after doing a Git refresh, IntelliJ IDEA complained that my project wasn't compiling clean any more. The culprit:

import javax.naming.Context;;

IntelliJ complains:

Error:(33, 29) java: Syntax error on token ";", delete this token

That code was pushed by a person who is not using IntelliJ, and it passed our backend build.

Is javac at fault, or IntelliJ? And what would be the document/specification that clarifies whether the above code should be an error or a warning?

This is IntelliJ 2019.2 CE EAP, running on macOS.

And just to be precise: there seems to be a mismatch between the IntelliJ "editor compile", and the result of hitting the "build" action. Fun fact: we have "use eclipse compiler" in our project setup. Changing it that to use javac fixes the problem; the double ;; is just a warning then.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • extra semicolons in import statements... interesting. – Johannes Kuhn Nov 08 '19 at 10:07
  • Which versions you are using? – Youcef LAIDANI Nov 08 '19 at 10:09
  • It's from IntelliJ, mine doesn't have this issue. I can put a thousand semicolon after an import statement – Ruelos Joel Nov 08 '19 at 10:10
  • @YCF_L See my update. – GhostCat Nov 08 '19 at 10:11
  • I tried this on Intellij 2019.2.4 on a mac. I didn't get this error. Only a warning. Perhaps you have your preferences set more strictly. – khelwood Nov 08 '19 at 10:14
  • @JohannesKuhn It's not 'in an import statement'. The import statement finished at the first semicolon. It is a free semicolon occuring at global scope, where only imports or interface/classe/annotation declarations are valid. In most contexts it is just an empty statement, but statements aren't valid here. Why the Java conmpiler allows it here remains a mystery. – user207421 Nov 08 '19 at 10:17
  • I try to find the relevant JLS part for this. But I have never thought about what it allowed in a single .java file. – Johannes Kuhn Nov 08 '19 at 10:18
  • 2
    It's a good question. If I were you I would look for an answer that points out to the JSL and explains why `;` allowed in the import declaration, in the type declaration (but not in the package declaration - you can't put a `;` before the `package` part) – Andrew Tobilko Nov 08 '19 at 10:28

3 Answers3

4

Unnecessary semicolons are not an error. They are considered as empty statements. And an empty statement, which does nothing, is not an error.

Cross check your code. It might also be IntelliJ at fault too. Its linting service might not be considering it as a legal statement.

The question is similar to this

Edit 1: The IntelliJ has an option in it's settings to which posts redundant semicolons as error.

3

Disclaimer

This does not resolve the issue, as it turns out the error occurs in relation to usage of the Eclipse compiler (as opposed to the default javac compiler).

This might still be useful to troubleshoot / highlight the inspection customization feature of IntelliJ.


You can set the level of severity for unnecessary semicolons (among many other things) in IntelliJ, in the Inspections profile.

The default setting for redundant semi-colons is "warning". Probably yours has been set to "Error".

enter image description here

This should not impact on the Java compiler, as redundant semi-colons are not a syntactic error.

Mena
  • 47,782
  • 11
  • 87
  • 106
  • @GhostCatsaysReinstateMonica ugh. Maybe some linter plugin? :S – Mena Nov 08 '19 at 10:17
  • The message in the question looks like it breaks the build, but setting this inspection to "error" doesn't. And the message is a bit different (just "unnecessary semicolon ';'"). – Tom Nov 08 '19 at 10:18
  • @Tom mmh. You're right, I can't find a way to configure the built-in inspector to replicate the problem exactly. I'm not sure it actually breaks the build for the OP though. Maybe this is due to some external plugin like SonarLint... – Mena Nov 08 '19 at 10:25
  • 1
    Ghostcode edited the question and mentioned what the issue is: using the eclipse compiler (you can setup IDEA to that as well). The editor still only shows a warning (or error when the inspection is set up to that), but the build then breaks. – Tom Nov 08 '19 at 10:56
  • @Tom interesting. Never used that option myself. Might be a legit bug in the eclipse compiler then... – Mena Nov 08 '19 at 11:15
  • Maybe. Eclipse can raise an error on empty statements/unused semicolons, but I can't find the default settings used and even when excplicitly ignoring this semicolon using `-err:-semicolon` doesn't remove this issue. On the other hand `-err:semicolon` creates another issue and I couldn't find any other ECJ settings flag regarding semicolons. Either ECJ doesn't allow you to ignore such errors, or IDEA has an issue with the ECJ integration. (Or ECJ is bugged :D) – Tom Nov 08 '19 at 11:25
  • And just for the record: the neat thing with the eclipse compiler is: it keeps compiling, instead of stopping on the first error. That is very convenient when you do TDD, and start to modify one class. You can still its tests, albeit *other* classes wouldn't compile any more. – GhostCat Nov 08 '19 at 11:39
  • @Tom thanks for furthering the research - I had already given up tbh. Related: I'm tempted to delete my answer as it does not technically answer GhostCat's question, but it kinds of sheds light on a slightly obscure IntelliJ feature though... Any recommendation? – Mena Nov 08 '19 at 11:43
  • @GhostCatsaysReinstateMonica well that explains that. Not related but is it worth it in your opinion? Haven't really done TDD *that seriously* myself but wouldn't be against trying with the right tools. – Mena Nov 08 '19 at 11:45
  • @GhostCatsaysReinstateMonica fair enough. I'll add a disclaimer. – Mena Nov 08 '19 at 11:50
3

According to the Java Language Specification §7.6 ; is a valid Type Declaration:

A top level type declaration declares a top level class type (§8 (Classes)) or a top level interface type (§9 (Interfaces)).

TypeDeclaration:

Extra ";" tokens appearing at the level of type declarations in a compilation unit have no effect on the meaning of the compilation unit. Stray semicolons are permitted in the Java programming language solely as a concession to C++ programmers who are used to placing ";" after a class declaration. They should not be used in new Java code.

If there is no setting in InteliJ that would change this behavior, then the extra semicolon should not produce an error.

Johannes Kuhn
  • 14,778
  • 4
  • 49
  • 73
  • 1
    you can put a `;` before an import declaration which makes it not a `TypeDeclaration` – Andrew Tobilko Nov 08 '19 at 10:31
  • Now that is interesting. In that case, the compiler should reject that code then. Any source (JLS) that this is legal? – Johannes Kuhn Nov 08 '19 at 10:33
  • No, it compiles fine. The inconsistency I found (with no references in the JLS) is that a `;` is allowed in import declarations but not in the package declaration. I assume the package declaration is always excepted to be a single statement, so even these dummy empty statements are being rejected there. – Andrew Tobilko Nov 08 '19 at 10:41