S1764, specifically regarding the << operator

For S1764, I can see there is some value to the checks other than the << operator.

For example, I have code that sends data to an SPI connected ADC, the channel number is requested with a 2 bit address in LSBits 4…3, so for channel 0 it’s (0 << 3), for channel 1 it’s (1 << 3), for channel 2 it’s (2 << 3) and for channel 3 it’s not allowed because who would ever want to do that??? Um, that would be me, as I want to use all 4 channels of the ADC, so (3 << 3).

Yes I could just write the hex directly, but then the code is less obvious.

I can’t actually think of a good example of a problem this is trying to fix, please share an example that makes sense.

Is there anyway to disable just the ‘a << a’ check?

1 Like

Also the wording in the write (Java static code analysis: Identical expressions should not be used on both sides of a binary operator) up is really bad:
" * Similarly, left-shifting 1 onto 1 is common in the construction of bit masks, and is ignored."
1 << 1 is NOT shifting 1 ONTO 1, it’s shifting 1 LEFT BY 1, i.e.
int x = 0b00000001 << 1;
x would now be 2 or 0b00000010.

For my previous example:
(0 << 3) == 0b00000000 == 0
(1 << 3) == 0b00001000 == 8
(2 << 3) == 0b00010000 == 16
(3 << 3) == 0b00011000 == 24

1 Like

Hello @l0cky3r,

Thanks for your feedback, I do agree with you that the rule description for rule S1764 needs a rework, and the good news is that Sonar is proactively reviewing and rewriting all the rule descriptions with a new format.

I created SONARJAVA-4558 to fix the rule in case of left-shift. The left-shift should be excluded by the rule because the result is not obvious, while the right-shift should still be reported since the result is always 0.

Cheers

1 Like

Hi Angelo,

I think it’s worth mentioning that I found these issues while working with SonarLint in VS code, checking C source code, just in case the SonarJava issue does not cover the C language checker.

It’s of particular interest as we have recently got a license of SonarQube and I imagine before long we will not be able to push to repo’s when rule violations are detected.

Thanks,
David

1 Like

Thank you @l0cky3r for clarifying, I’ll ask for help from C specialists.

2 Likes

Hello @l0cky3r,

Thank you for the feedback. We already have a ticket to broaden the exception:
CPP-4234: S1764: Fix FP when a shift operator is applied on constants.
Do you think this would solve your issue?

As a side note, the link you provided redirects to the Java rule.
The C rule is: Identical expressions should not be used on both sides of a binary operator.
There are slight differences between them, as you can see, including the exceptions. But this rule will also be rewritten soon to improve it :slight_smile:

Best,
Amélie

1 Like

Hi Amelie,

First apologies for the confusion with the wrong link, I think I just googled sonar lint and the clause, but failed to notice I had ended up on Java.

Yes I think that CPP-4234: S1764 will cover it, thank you!

David

1 Like