Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are unnecessary semicolons syntax errors?

This morning, after doing a git refresh, IntelliJ 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.

Question: is javac at fault, or IntelliJ? And what would be the document/spec 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.

like image 451
GhostCat Avatar asked Dec 23 '22 20:12

GhostCat


2 Answers

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.

like image 174
Gobind Deep Singh Avatar answered Dec 26 '22 10:12

Gobind Deep Singh


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:

  • ClassDeclaration
  • InterfaceDeclaration
  • ;

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.

like image 25
Johannes Kuhn Avatar answered Dec 26 '22 10:12

Johannes Kuhn