>
>
>
V1115. Function annotated with the 'pur…


V1115. Function annotated with the 'pure' attribute has side effects.

The analyzer has detected a function annotated as pure, but it is not.

You can annotate functions in the following ways:

A function is pure if it meets the following requirements:

  • It has no side effects. A function should not alter the state of the program outside its own context. This means it should not modify objects with static storage duration (local and global) or modify non-constant objects via pointers/references passed to the function.
  • The function behavior is deterministic. A function must always return the same result for the same set of inputs.

Here are the most common cases in which a function purity is violated:

  • using variables with static storage duration in any form;
  • calling a function that has side effects;
  • using constructs that cause side effects (for example, 'new', 'delete');
  • using parameters as lvalue references or pointers to non-constants;
  • writing to/reading from streams (e.g. 'std::cout', 'std:: fstream', etc.).

Take a look at the following example of an impure function annotated as pure:

[[gnu::pure]] void foo() 
{
  int *x = new int;
  ....
}

The 'foo' function is annotated in the code using the 'gnu::pure' attribute but allocates dynamic memory and violates the "no side effects" requirement.

To fix this, either remove the 'pure' attribute or modify the function as follows:

[[gnu::pure]] void foo()
{
  int x;
  ....
}