Local variable is redundant javascript

javascript eslint local variable is redundant

I’m trying to find out how to suppress the eslint error in my javascript code.

Local variable ‘rtn’ is redundant

I found the documentation for this here: https://eslint.org/docs/rules/no-useless-return

If you don’t care about disallowing redundant return statements, you can turn off this rule.

But don’t say how to disable this error. The only example they give is having it enabled: /* eslint no-useless-return: «error» */ .

I’m new to eslint so how do I do this?
I’ve tried

/* eslint no-useless-return: «suppress» */
export function dateToYYYYMMDD(theDate : Date, separator = «-«) : string const rtn = theDate.getFullYear() + separator
+ («0» + (theDate.getMonth() + 1)).substr(-2) + separator
+ («0» + theDate.getDate()).substr(-2);
return rtn;
>

but this didn’t work. I tried other values like “ok” but that didn’t work.
What is the right value to use?

The same question but for Java is here: https://stackoverflow.com/questions/26436155/local-variable-is-redundant-using-java. The Java annotation @SuppressWarnings(«UnnecessaryLocalVariable») does not work in Javascript. 🙂

Answer

davy.ai

To disable the eslint error for redundant return statements, you can use the following comment at the top of your javascript file: /* eslint-disable no-useless-return */ This will disable the rule for the entire file. If you only want to disable the rule for a specific function or block of code, you can use the following comment before the function or block:

/* eslint-disable no-useless-return */ export function dateToYYYYMMDD(theDate : Date, separator = "-") : string < const rtn = theDate.getFullYear() + separator + ("0" + (theDate.getMonth() + 1)).substr(-2) + separator + ("0" + theDate.getDate()).substr(-2); return rtn; >/* eslint-enable no-useless-return */ 

This will disable the rule for the function, but enable it again after the function is defined to avoid disabling it for the rest of the file. For the value to use in the eslint comment, you should use “off” or “0” to completely disable the rule, or “warn” or “1” to enable the rule as a warning instead of an error. “suppress” or “2” are not valid values for this rule.

Читайте также:  Тег input, атрибут required

Источник

Local variable is redundant javascript

Профиль
Группа: Участник
Сообщений: 2
Регистрация: 4.12.2017

Всем добра. Изучаю js. И возникла ошибка, не могу понять как ее исправить.
Есть форма. При вводе ширины и длины считается площадь

Код
Введите длину прямоугольника

Введите ширину прямоугольника

Площадь прямоугольника равна

Код
function areaRectangle() var a=document.forma1.t1.value;
var b=document.forma1.t2.value;
var s=a*b;
document.forma1.res.value=s;
>

И вот ошибка возникает в переменной s — local variable s is redundant

Это сообщение отредактировал(а) maryka — 4.12.2017, 19:44

Профиль
Группа: Участник
Сообщений: 130
Регистрация: 14.12.2014

В каком браузере такое возникает?

Код
Введите длину прямоугольника

Введите ширину прямоугольника

Площадь прямоугольника равна

Код
function areaRectangle(f) var a = +f.namedItem(‘t1’).value || 0,
b = +f.namedItem(‘t2’).value || 0;

Источник

Code Inspections in JavaScript

In JavaScript, ReSharper 2023.1 provides two kinds of code inspections: 4 inspections that detect errors such as broken syntax, unresolved symbols, compiler errors, and so on (you cannot configure or disable any of these inspections), and 113 proprietary code inspections , any of which you can disable or change its severity level.
These code inspections help you detect code issues in design time in all open files, and additionally they allow you to find code issues in specific scope.

Configurable JavaScript inspections are listed below, grouped by their categories.

Common Practices and Code Improvements (3 inspections)

This category groups inspections that hunt for medium severity issues that mainly affect code readability.

Join local variable declaration and assignment

Statement termination does not match code style settings

Use of future reserved word

Constraints Violations (1 inspection)

This category includes code inspections, mostly with the warning severity level, which detect violations related to symbol attributes, including ReSharper’s code annotations, and other similar issues.

Language Usage Opportunities (9 inspections)

This category includes code inspections, mostly with the suggestion severity level, which notify you when more advanced language constructs can be used. These inspections detect syntax of outdated language versions and suggest using features from more modern language versions. For most of the supported languages, language version can be detected automatically or set manually.

A series of undefined-checks for properties can be replaced with destructuring

Intermediate local variable is redundant and can be safely inlined

Intermediate local variable is redundant because it can be replaced with a destructuring swap expression

Local variable can be safely moved to inner block

String concatenation can be converted to a template string

Subsequent indexers can be replaced with a destructuring declaration or assignment

Subsequent indexers in object literal fields can be simplified with destructuring

‘var’ variable can be made ‘let’ variable

Variable can be made constant

Potential Code Quality Issues (68 inspections)

This category includes inspections that detect critical issues (code smells), mostly with Error or Warning level. This category also includes inspections that ensure localization assistance.

A trailing element of an array is elided

Assignment to a variable inside a conditional statement

Assignment to an implicitly declared global variable

Источник

Local variable is redundant JetBrains IDEs (e.g. IntelliJ IDEA)

At this point you’ve created the variable but never assigned a value to it. The first time you use this variable is in your return statement:

return (amount = reader.nextDouble()); 

What you should’ve done is either assign the variable on the same line as the declaration:

public static double getAmount()

Or, better still, don’t use the variable at all (although it may improve code readability, in which case a valid point could be made to keep it):

public static double getAmount()

Why is Intellij warning me?
Intellij is just telling you that in the end your variable isn’t being used (it isn’t) so it’s safe to remove the variable declaration.

enter image description here

Intellij should however also provide you with a solution like I just did if you hover over the little lightbulb which appears at the start of your line of code. An example:

Solution 2

It’s telling you that you can simply return reader.nextDouble() and that you don’t need the variable amount for that purpose.

Solution 3

Because you aren’t doing anything with it. The code could be simplified to:

public static double getAmount()

Solution 4

Try turning Inspection Mode off for redundant local variables. Or you can just simply do :

Источник

Оцените статью