question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

no-useless-return and no-empty conflict

See original GitHub issue

Tell us about your environment

  • ESLint Version: 5.6.1
  • Node Version: 10.11.0
  • npm Version: 6.4.1

What parser (default, Babel-ESLint, etc.) are you using? default Please show your full configuration: My configuration is insanely elaborate covering most of the rules in depth, for an easy to verify example use this

What did you do? Please include the actual source code causing the issue, as well as the command that you used to run ESLint. See the demo for a live example of source code that causes this issue, try clearing line 3.

eslint . --ext .js

What did you expect to happen? One of the two wouldn’t error, in order to avoid conflict which each other. Even --fix conflicts with these rules. What actually happened? Please include the actual, raw output from ESLint. without --fix

../src/utility/LibraryHandler.js
  65:11  error  Unnecessary return statement  no-useless-return

with --fix

../src/utility/LibraryHandler.js
  64:46  error  Empty block statement  no-empty

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
not-an-aardvarkcommented, Oct 10, 2018

To clarify, if code after the if/else statement isn’t supposed to be executed, then using return is fine (and no-useless-return won’t report an error if there are additional statements).

Even if there aren’t additional statements after the if/else, if the first case is conceptually supposed to be an “early exit” it might be clearer to write it this way:

function func(bool, num) {
	if(bool) {
		return;
	}

        if(num === 5) {
		console.log("yay");
	} else if(num > 0) {
		console.log("rip");
	}
}

If the first case isn’t conceptually an “early exit” and return is being used as a placeholder for “do nothing”, then it seems clearer to actually do nothing (perhaps with a comment clarifying this choice), otherwise the use of return could be misleading to people reading/modifying the code in the future.

1reaction
not-an-aardvarkcommented, Oct 9, 2018

@LJNeon The reasoning for adding a comment is that it can help explain to readers of your code why the block is empty, when it might otherwise appear that it was a mistake. For example, you could write something like this:

function func(bool, num) {
	if(bool) {
		// do nothing
	} else if(num === 5) {
		console.log("yay");
	} else if(num > 0) {
		console.log("rip");
	}
}

In other words, adding a comment to the block is less of a workaround to appease the linter, and more of a mechanism for making sure your code is readable. Using return; in this case isn’t ideal because it could cause unexpected results if you need to add additional statements after the if/else statement (namely, those statements will be skipped).

Read more comments on GitHub >

github_iconTop Results From Across the Web

no-useless-return - ESLint - Pluggable JavaScript Linter
A pluggable and configurable linter tool for identifying and reporting on patterns in JavaScript. Maintain your code quality with ease.
Read more >
@studiometa/eslint-config - npm package | Snyk
no-empty-function, 'error' ... no-useless-return, 'warn' ... plugin turns off all ESLint stylistic rules that might conflict with Prettier.
Read more >
eslint/CHANGELOG.md - UNPKG
1007, * 4d35a81 Fix: Add a utility to avoid autofix conflicts (fixes #7928, ... 1125, * ca1f841 Fix: no-useless-return stack overflow on loops...
Read more >
Code Issues - Embold Help Center
AvoidEnumAsIdentifier, Use of the term 'enum' will conflict with newer versions of Java ... no-empty, Empty blocks are often indicators of missing code....
Read more >
hterm and Secure Shell - 1.92.1, 2022-03-04, Minor bug fixes.
... eslint: Enable no-empty check. eslint: Enable prefer-rest-params check. ... eslint: Enable no-useless-return checks. eslint: Enable no-useless-escape ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found