emscripten-core / emscripten-core/emscripten

`-Wignored-qualifiers` produces an incorrect warning about `const` return type qualifier

Open
#11,123 8 comments 0 reactions 0 assignees View on GitHub
pinned
Dominant language
C++
Stars
27.6k
Forks
3.6k
Avg merge
1d 1h
Merged PRs (30d)
105

Description

Consider the following example:

```c++
#include

struct Foo {
Foo() { std::cout << "Foo()\n"; }
Foo(const Foo&) { std::cout << "Foo(const Foo&)\n"; }
Foo(Foo&&) { std::cout << "Foo(Foo&&)\n"; }
~Foo() { std::cout << "~Foo()\n"; }
};

struct Bar : Foo {
};

const auto f() {
return Bar();
}

void bar(const Foo) {
std::cout << "bar()\n";
}

int main() {
bar(f());
}
```

It should produce the following output:
```
Foo()
Foo(const Foo&)
bar()
~Foo()
~Foo()
```
Note that copy elision in `bar(f())` is not permitted as `f()` returns `Bar` and `bar()` takes `Foo`. Hence, a copy or move constructor should be called instead. As `f()` returns a const value, only copy constructor is allowed.

However, `em++ -std=c++17 -Wall -Wextra a.cpp -o a.js` gives a false positive warning:
```
a.cpp:13:1: warning: 'const' type qualifier on return type has no effect
[-Wignored-qualifiers]
const auto f() {
^~~~~~
1 warning generated.
```

If I replace `const auto` with `const Bar`, the warning goes away.

I'm able to reproduce this issue with some Clang version as well:

```
clang version 10.0.0
Target: x86_64-pc-windows-msvc
Thread model: posix
InstalledDir: F:\Soft\LLVM\bin
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.