# Wrong suggestion to use pointer to const

**URL:** <https://community.sonarsource.com/t/wrong-suggestion-to-use-pointer-to-const/58181>\
**Category:** Report False-positive / False-negative...\
**Tags:** cfamily\
**Created:** [February 11, 2022, 2:11pm UTC](https://community.sonarsource.com/t/wrong-suggestion-to-use-pointer-to-const/58181 "2022-02-11T14:11:02Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![torgeir.skogen](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/torgeir.skogen/32/20976_2.png) [@torgeir.skogen](https://community.sonarsource.com/u/torgeir.skogen)\
**Post date:** [February 11, 2022, 2:11pm UTC](https://community.sonarsource.com/t/wrong-suggestion-to-use-pointer-to-const/58181/1 "2022-02-11T14:11:02Z")

</div>

I found this case via SonarQube 9.3 and reproduced it with SonarLint 5.4.0.42421

```cpp
struct Bar
{
    int *p;
};

inline auto foo1(int *const ptr)
{
    return Bar(ptr);
}

inline auto foo2(int *const ptr)
{
    return Bar{ptr};
}

```

I originally found this in a code snippet that involves `std::make_unique` and polymorphism, but I managed to reproduce it here.

 ![image](https://europe1.discourse-cdn.com/sonarsource/uploads/sonarcommunity/original/3X/9/b/9bb5a206b70fcbc640b3eed784910d840fbad41a.png)

It seems like the analyzer doesn’t understand that the pointer cannot be a pointer to const when calling constructors with parentheses. But it somehow understands it when using braces? It seems weird for the analyzer to not understand this important aspect of the type system. A pointer to const is not convertible to a pointer to non-const, so this code would not compile if I added the `const` as suggested.

The same seems to apply for references.

---

<div class="post-metadata">

**Author:** ![necto](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/necto/32/14722_2.png) [@necto](https://community.sonarsource.com/u/necto)\
**Post date:** [February 14, 2022, 8:52am UTC](https://community.sonarsource.com/t/wrong-suggestion-to-use-pointer-to-const/58181/3 "2022-02-14T08:52:08Z")

</div>

Hi @torgeir.skogen,  
Thank you for reporting the suspected false positive. In this case, I do not consider it to be a false positive, because your code fails to compile:

> error: no matching conversion for functional-style cast from ‘int \*const’ to ‘Bar’

Given the code is incorrect from a C++ compiler point of view, our analyzer does its best to interpret the intent and report the potential issues once the code is fixed. Surely enough, it often fails to do so. However, it does flag the code with a special issue.

There is a rule with `ParsingError` as a key. You can enable this rule (through the SonarLint JSON setting or in your SQ instance) to make sure that there is no parsing error in the code where the false positive is happening. For example, in SonarLint for VisualStudio, you can put the following into your `settings.json`:

```auto
{
  "sonarlint.rules": {
    "cpp:ParsingError": {
      "level": "On"
    },
    "c:ParsingError": {
      "level": "On"
    }
  }
}

```

You can fix `foo1` either the way you did in `foo2` or by adding a constructor to `Bar` that would take a non-const pointer. Both fixes also remove the S995 report.

However, you mentioned that you’ve encountered the FP in a more complex piece of code. I encourage you to get back to your original code and check if it contains a `ParsingError`. If it does not, besides trying to simplify it again and watching to not introduce a `ParsingError`, you can generate a reproducer file:

- Add the reproducer option to the scanner configuration:  
sonar.cfamily.reproducer= “Full path to the .cpp file that has or include the file that has the false-positive”
- Re-run the scanner to generate a file named `sonar-cfamily.reproducer` in the project folder.
- Please share this file. If you think this file contains private information you can send it privately.

---

<div class="post-metadata">

**Author:** ![torgeir.skogen](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/torgeir.skogen/32/20976_2.png) [@torgeir.skogen](https://community.sonarsource.com/u/torgeir.skogen)\
**Post date:** [February 14, 2022, 3:26pm UTC](https://community.sonarsource.com/t/wrong-suggestion-to-use-pointer-to-const/58181/4 "2022-02-14T15:26:43Z")

</div>

The code compiles in C++20 mode. [Compiler Explorer](https://godbolt.org/z/x9Gh5a576) because you are now allowed to initialize aggregates with parenthesis.

Good call on the parsing errors. I enabled those in the quality profile in the projects I manage.

I think I managed to land this reproducer

```cpp
struct Base
{};

struct Bar : Base
{
    explicit Bar(int *);
};

inline std::unique_ptr<Base> factory(int *const p)
{
    return std::make_unique<Bar>(p);
}

```

Which seems to emit the same false positive, but not a parsing error.

 ![image](https://europe1.discourse-cdn.com/sonarsource/uploads/sonarcommunity/original/3X/5/5/551533714500f5fc4e1972830eca3b994bae247e.png)

---

<div class="post-metadata">

**Author:** ![necto](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/necto/32/14722_2.png) [@necto](https://community.sonarsource.com/u/necto)\
**Post date:** [February 15, 2022, 2:29pm UTC](https://community.sonarsource.com/t/wrong-suggestion-to-use-pointer-to-const/58181/5 "2022-02-15T14:29:18Z")

</div>

You are right, your simplified code produces a FP, which I recorded in this ticket: [CPP-3545](https://jira.sonarsource.com/browse/CPP-3545). The root cause is the [missing implementation](https://clang.llvm.org/cxx_status.html#cxx20) of “Parenthesized initialization of aggregates” in clang 13 that we use as a frontend. Thank you for reporting it!

However, I was not able to reproduce FP on your original code with `unique_ptr`. There is certainly something in your compiler configuration or surrounding code that I am missing.

To nail down the problem I will need a “reproducer” file that collects all the information necessary for me to reproduce an issue. See [these instructions](https://github.com/SonarSource/sonarlint-visualstudio/wiki/Troubleshooting-problems-with-C-or-Cpp-analysis#creating-a-reproducer-file-for-the-active-document) on how to generate it. If you worry about sharing potentially sensitive information from your build environment you can send it to me privately.

---

<div class="post-metadata">

**Author:** ![necto](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/necto/32/14722_2.png) [@necto](https://community.sonarsource.com/u/necto)\
**Post date:** [February 16, 2022, 1:03pm UTC](https://community.sonarsource.com/t/wrong-suggestion-to-use-pointer-to-const/58181/6 "2022-02-16T13:03:14Z")

</div>

As discussed privately, while the reproducer for the original false-positive remains elusive, @torgeir.skogen found another issue, this time a false-negative: [CPP-3548](https://jira.sonarsource.com/browse/CPP-3548).

Thank you again. This thread has been quite productive so far 🙂

Let me know if you manage to create a reproducer for the original issue or have other questions.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/system/32/31083_2.png) [@system](https://community.sonarsource.com/u/system)\
**Post date:** [June 2, 2022, 9:41am UTC](https://community.sonarsource.com/t/wrong-suggestion-to-use-pointer-to-const/58181/7 "2022-06-02T09:41:47Z")

</div>

Closed on June 2, 2022, 9:41am UTC

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.
