cpp:S1854 Value stored during its initialization is never read (in inline function)

  • What language is this for? C++23, cmake, MSVC, no build-wrapper

  • Which rule? S1854

  • Why do you believe it’s a false-positive/false-negative?
    In the next line the value is used :wink:

  • SonarQube Cloud. SonarCloudPrepare@4.2.0

  • SonarScanner CLI 8.0.1.6346

  • How can we reproduce the problem? Give us a self-contained snippet of code (formatted text, no screenshots)

	static inline double Length(const PK_VECTOR_s v) noexcept {
		const auto squareLength = v.coord[0] * v.coord[0] + v.coord[1] * v.coord[1] + v.coord[2] * v.coord[2];
// Value stored to 'squareLength' during its initialization is never read

		return std::sqrt(squareLength);
	}

where from an external (C) library h file

struct PK_VECTOR_s
    {
    double coord[3];
    };
typedef struct PK_VECTOR_s PK_VECTOR_t;

Hi @milbrandt,

This kind of issue usually happen because of a parsing error during the analysis. What the analyzer looks at is the code where the problematic parts have been removed, and therefore it might not see where the value is read.

Here is for instance a possible scenario:

  • The header is not found during analysis
  • PK_VECTOR_s is therefore unknown
  • As a consequence, the type of squareLength is not properly deduced, and is replaced by an error type
  • The call to sqrt with an error type fails, and the call is removed from the analyzed code.
  • So the analyzer sees squareLength as never read.

Can you enable sonar.verbose=true and see if there are parsing errors related to this piece of code? Maybe you can solve them on your side (for instance if because of configuration, the header that describes PK_VECTOR_s is not correctly included), otherwise you can share it with us.

It is a C++ module (ixx) file and I get several missing references. But external_kernel.h where the PK_VECTOR is defined seems to be found.

module;

#include <vector>
#include <memory>
#include <string>
#include <numbers>
#include <cmath>
#include <external_kernel.h>

export module MyModule;

15:41:39.166 DEBUG D:_1\10\s\source\Backend\src\geometry\GeometryApi.ixx:3 - ‘vector’ file not found
15:41:39.166 DEBUG D:_1\10\s\source\Backend\src\geometry\GeometryApi.ixx:4 - ‘memory’ file not found
15:41:39.166 DEBUG D:_1\10\s\source\Backend\src\geometry\GeometryApi.ixx:5 - ‘string’ file not found
15:41:39.166 DEBUG D:_1\10\s\source\Backend\src\geometry\GeometryApi.ixx:6 - ‘numbers’ file not found
15:41:39.166 DEBUG D:_1\10\s\source\Backend\src\geometry\GeometryApi.ixx:7 - ‘cmath’ file not found
15:41:39.166 DEBUG D:_1\10\s\source\Backend\src\geometry\GeometryApi.ixx:23 - use of undeclared identifier ‘std’
15:41:39.170 DEBUG D:_1\10\s\source\Backend\src\geometry\GeometryApi.ixx:D:\Parasolid\381\x64_win\base\external_kernel.h:10 - ‘stddef.h’ file not found

The cleaned up signature static inline double Length(const PK_VECTOR_t& v) noexcept also didn’t helped (as expected).

You are missing <cmath>, which is where sqrt is defined. which can explain why the sqrt call is removed from the analysis…

It is in fact worse than that: You are missing the full standard library:

use of undeclared identifier ‘std’

So in addition to the issue you mentioned, there are probably numerous other issues that are no so visible but greatly decrease the quality of the analysis.

How do you configure the analysis? You say you are not using the build wrapper: Are you using autoscan or are you creating the compile_commands.json in another way? In that case, can you send it to us so we can have a look at it?

Yes, I saw that in the logs - but it compiles, therefore it is probably an issue of the analysis.

The build is done with cmake in Developer Powershell of Visual Studio 2026 on the Azure pipelines build server. Main steps in Powershell are

cmake --preset $(backendPreset) -DCMAKE_EXPORT_COMPILE_COMMANDS=ON
cmake --build --preset $(backendPreset) --parallel

The build I reduced a lot, especially the private vcpkg registry and boost dependencies are removed. The now generated compile_commands.json is attached, in the verbose logs I still see the not found / undeclared identifiers. The file starts with

module;

#include <vector>
#include <memory>
#include <string>
#include <numbers>
#include <cmath>
#include <XXX_kernel.h>

export module GeometryApi;

compile_commands.zip (1.2 KB)

Hi @milbrandt,

We would need the full logs to better understand what happens. At the start of the log are precious informations about how we deduce the system include paths when we analyze a MSVC project.

Thank you!

Hi @JolyLoic

Attached you’ll find in the zip the full log of the code analyze step (10.txt) and some additional files I extracted from the build server which might help to understand what is going on.

For more files we should switch to a private channel.

S1854-analysis.zip (124.2 KB)

There are different heuristics we use to determine where to look for system headers when working with MSVC. From what I see in the log, the one that is used in your case is an environment variable named INCLUDE.

It is apparently defined in your analysis environment, but not with the right value. On my machine, it looks like:

C:\Program Files\Microsoft Visual Studio\18\Enterprise\VC\Tools\MSVC\14.50.35717\include;
C:\Program Files\Microsoft Visual Studio\18\Enterprise\VC\Tools\MSVC\14.50.35717\ATLMFC\include;
C:\Program Files\Microsoft Visual Studio\18\Enterprise\VC\Auxiliary\VS\include;
C:\Program Files (x86)\Windows Kits\10\include\10.0.26100.0\ucrt;
C:\Program Files (x86)\Windows Kits\10\\include\10.0.26100.0\\um;
C:\Program Files (x86)\Windows Kits\10\\include\10.0.26100.0\\shared;
C:\Program Files (x86)\Windows Kits\10\\include\10.0.26100.0\\winrt;
C:\Program Files (x86)\Windows Kits\10\\include\10.0.26100.0\\cppwinrt;
C:\Program Files (x86)\Windows Kits\NETFXSDK\4.8\include\um

And it is typically set when running a script like VsDevCmd.bat or opening a Developper command prompt.

If you know why this variable is not correctly set in your analysis, maybe you can fix it, otherwise, we’ll need a reproducer for the file with the errors to get more info. I’m sending you a PM in case this is the next step.