SA0184 : Redundant pairs of parentheses can be removed

The topic describes the SA0184 analysis rule.

Message

Redundant pairs of parentheses can be removed

Description

The rule reports logical and arithmetic expressions, and queries for being enclosed in redundant pairs of parentheses.

The parentheses, which are not used to specify the desired order of operations could be misleading and should be removed.

How to fix

Remove the redundant pair of parentheses.

Scope

The rule has a Batch scope and is applied only on the SQL script.

Parameters
Name Description Default Value
IgnoreArithmeticExpressionMainParentheses

Specifies whether the main parentheses, which wrap an arithmetic expression to be ignored.

yes

IgnoreBooleanExpressionMainParentheses

Specifies whether the main parentheses, which wrap an logical expression to be ignored.

yes

Remarks

The rule does not need Analysis Context or SQL Connection.

Effort To Fix
2 minutes per issue.
Categories

Design Rules, Code Smells

Additional Information

There is no additional info for this rule.

Example Test SQL
SQL
 1CREATE TABLE Test.Greeting
 2(
 3GreetingId INT IDENTITY (1,1) PRIMARY KEY,
 4Message nvarchar(255) NOT NULL,
 5)
 6
 7INSERT INTO Test.Greeting (Message) 
 8SELECT ('Hello, world!')
 9
10INSERT INTO Test.Greeting (Message) 
11VALUES ('How do yo do?'),
12        ('Good morning!'),
13        ('Good night!')
14
15DELETE  Test.Greeting WHERE (GreetingId = 3 and (aaa != 0 and ttt = 12) and a <1) or a = 1
16
17select (6/(7/5)*12) + 1
18select ((1 + 2) - 3) * 4  / 5 -(6 /(7/5)*12 ) 
19
20SELECT lower('AA'),(g.col1 + (g.col2)) FROM Test.Greeting g 
21WHERE 
22g.Message like ('Hello%')
23or g.Message in (((((select message from UserMessges)))))
24
25DROP TABLE Test.Greeting
26declare @a int = 5
27
28DECLARE @b INT 
29set @b = (@a / 2 + 1);  
30IF (@b > 0) AND ((@b+@a > 0)) 
31BEGIN
32 print '1'
33END
34
35set @b   = (@a / 3 + 1);
36IF (@b > 0)
37BEGIN
38 print '2'
39END
40
41IF (@b > 0) AND (@b-@a > 0) AND (@b-@a) > 0
42BEGIN
43 print '2'
44END

Example Test SQL with Automatic Fix
SQL
 1CREATE TABLE Test.Greeting
 2(
 3GreetingId INT IDENTITY (1,1) PRIMARY KEY,
 4Message nvarchar(255) NOT NULL,
 5)
 6
 7INSERT INTO Test.Greeting (Message) 
 8SELECT 'Hello, world!'
 9
10INSERT INTO Test.Greeting (Message) 
11VALUES ('How do yo do?'),
12        ('Good morning!'),
13        ('Good night!')
14
15DELETE  Test.Greeting WHERE (GreetingId = 3 and aaa != 0 and ttt = 12 and a <1) or a = 1
16
17select (6/7/5*12) + 1
18select (1 + 2 - 3) * 4  / 5 -(6 /7/5*12 ) 
19
20SELECT lower('AA'),(g.col1 + g.col2) FROM Test.Greeting g 
21WHERE 
22g.Message like 'Hello%'
23or g.Message in ((((select message from UserMessges))))
24
25DROP TABLE Test.Greeting
26declare @a int = 5
27
28DECLARE @b INT 
29set @b = (@a / 2 + 1);  
30IF @b > 0 AND (@b+@a > 0) 
31BEGIN
32 print '1'
33END
34
35set @b   = (@a / 3 + 1);
36IF (@b > 0)
37BEGIN
38 print '2'
39END
40
41IF @b > 0 AND @b-@a > 0 AND (@b-@a) > 0
42BEGIN
43 print '2'
44END

Analysis Results
  Message Line Column
1 SA0184 : Redundant pairs of parentheses can be removed. 8 7
2 SA0184 : Redundant pairs of parentheses can be removed. 15 48
3 SA0184 : Redundant pairs of parentheses can be removed. 17 10
4 SA0184 : Redundant pairs of parentheses can be removed. 18 8
5 SA0184 : Redundant pairs of parentheses can be removed. 18 35
6 SA0184 : Redundant pairs of parentheses can be removed. 22 15
7 SA0184 : Redundant pairs of parentheses can be removed. 20 29
8 SA0184 : Redundant pairs of parentheses can be removed. 22 15
9 SA0184 : Redundant pairs of parentheses can be removed. 23 17
10 SA0184 : Redundant pairs of parentheses can be removed. 30 3
12 SA0184 : Redundant pairs of parentheses can be removed. 41 3
13 SA0184 : Redundant pairs of parentheses can be removed. 41 16
See Also

Other Resources