NewlineAtEndOfFile

Since Checkstyle 3.1

Description

Checks whether files end with a line separator.

Rationale: Any source files and text files in general should end with a line separator to let other easily add new content at the end of file and "diff" command does not show previous lines as changed.

Example (the line with 'No newline at end of file' should not be in the diff):

@@ -32,4 +32,5 @@ ForbidWildcardAsReturnTypeCheck.returnTypeClassNamesIgnoreRegex
PublicReferenceToPrivateTypeCheck.name = Public Reference To Private Type

StaticMethodCandidateCheck.name = Static Method Candidate
-StaticMethodCandidateCheck.desc = Checks whether private methods should be declared as static.
\ No newline at end of file
+StaticMethodCandidateCheck.desc = Checks whether private methods should be declared as static.
+StaticMethodCandidateCheck.skippedMethods = Method names to skip during the check.
        

It can also trick the VCS to report the wrong owner for such lines. An engineer who has added nothing but a newline character becomes the last known author for the entire line. As a result, a mate can ask him a question to which he will not give the correct answer.

Old Rationale: CVS source control management systems will even print a warning when it encounters a file that doesn't end with a line separator.

Attention: property fileExtensions works with files that are passed by similar property for at Checker. Please make sure required file extensions are mentioned at Checker's fileExtensions property.

Notes

This will check against the platform-specific default line separator.

It is also possible to enforce the use of a specific line-separator across platforms, with the lineSeparator property.

Properties

name description type default value since
fileExtensions Specify the file extensions of the files to process. String[] all files 3.1
lineSeparator Specify the type of line separator. LineSeparatorOption lf_cr_crlf 3.1

Examples

To configure the check:

<module name="Checker">
  <module name="NewlineAtEndOfFile"/>
</module>
        

Correct Example:

public class Example1 { // ⤶
// ⤶
} // ⤶ // ok, file ends with a new line.
        

Violation Example:

public class Example2 { // ⤶
// ⤶
} // no ⤶ below it is violation
        

To configure the check to always use Unix-style line separators:

<module name="Checker">
  <module name="NewlineAtEndOfFile">
    <property name="lineSeparator" value="lf"/>
  </module>
</module>
        

Example:

public class Example3 { // ⤶
} // ␍⤶  it is violation as line ending is different from expected
        

To configure the check to work only on Java, XML and Python files:

<module name="Checker">
  <module name="NewlineAtEndOfFile">
    <property name="fileExtensions" value="java, xml, py"/>
  </module>
</module>
        

Python file Example4.py:

print("Hello world!")
# no ⤶ below it is violation
        

Java file Example5.java:

public class Example5 { // ⤶
// ⤶
} // no ⤶ below it is violation
        

Text file Example6.txt:

Sample txt file. // ok, this file is not specified in the config.
        

Example of Usage

Violation Messages

All messages can be customized if the default message doesn't suit you. Please see the documentation to learn how to.

Package

com.puppycrawl.tools.checkstyle.checks

Parent Module

Checker