-
Type:
Code Smell Detection
-
Status: Active
-
Resolution: Unresolved
-
Labels:
-
Message:Extract this [increment|decrement] operation into a dedicated statement.
-
Highlighting:
- Primary: operator
-
Default Severity:Major
-
Impact:Low
-
Likelihood:High
-
Legacy Key:IncAndDecMixedWithOtherOperators
-
Targeted languages:Flex
-
Covered Languages:C#, C, C++, Java, JavaScript, Objective-C, PHP, Swift, TypeScript
-
Irrelevant for Languages:ABAP, Cobol, Go, HTML, PL/I, PL/SQL, Python, RPG, T-SQL, VB.Net, VB6, XML
-
Remediation Function:Constant/Issue
-
Constant Cost:5min
-
Analysis Level:Syntactic Analysis
-
Analysis Scope:Main Sources, Test Sources
-
Common Rule:Yes
-
CERT:EXP30-C., EXP50-CPP., EXP05-J.
-
PC-Lint:9049, 9163
-
PMD:AvoidMultipleUnaryOperators
-
TSLint-SonarTS:no-nested-incdec
The use of increment and decrement operators in method calls or in combination with other arithmetic operators is not recommended, because:
- It can significantly impair the readability of the code.
- It introduces additional side effects into a statement, with the potential for undefined behavior.
- It is safer to use these operators in isolation from any other arithmetic operators.
Noncompliant Code Example
u8a = ++u8b + u8c--; foo = bar++ / 4;
Compliant Solution
The following sequence is clearer and therefore safer:
++u8b; u8a = u8b + u8c; u8c--; foo = bar / 4; bar++;
See
- CERT, EXP30-C. - Do not depend on the order of evaluation for side effects
- CERT, EXP50-CPP. - Do not depend on the order of evaluation for side effects
- CERT, EXP05-J. - Do not follow a write by a subsequent write or read of the same object within an expression
- deprecates
-
RSPEC-1454 Multiple "++" or "--" unary operators should not be used in a single arithmetic expression
- Deprecated
- is implemented by
-
SONARFLEX-99 Rule: Increment (++) and decrement (--) operators should not be used in a method call or mixed with other operators in an expression
-
- Open
-
-
SONARSLANG-77 Rule S881: Increment (++) and decrement (--) operators should not be used in a method call or mixed with other operators in an expression
-
- Open
-
-
CPP-273 The increment (++) and decrement (--) operators should not be mixed with other operators in an expression (MISRA C 12.13, MISRA C++ 5-2-10)
-
- Closed
-
-
SONARSWIFT-82 Rule: Increment (++) and decrement (--) operators should not be mixed with other operators in an expression
-
- Closed
-
-
SONARJS-105 Rule Increment (++) and decrement (--) operators should not be mixed with other operators in an expression
-
- Closed
-
-
CPP-707 Create Objective-C rules repository containing targeted rules
-
- Closed
-
-
CPP-1038 Fix false negative in IncAndDecMixedWithOtherOperators
-
- Closed
-
- links to
1.
|
Swift | RSPEC-2632 |
|
Active | Unassigned | |
2.
|
PHP | RSPEC-3212 |
|
Active | Unassigned | |
3.
|
C-Family | RSPEC-3678 |
|
Active | Unassigned | |
4.
|
JavaScript | RSPEC-3978 |
|
Active | Unassigned |