Skip to content

Commit e0477f6

Browse files
committed
Merge #8794: Enable -Wshadow by default
359bac7 Add notes about variable names and shadowing (Pavel Janík) fd5654c Check and enable -Wshadow by default. (Pavel Janík)
2 parents e81df49 + 359bac7 commit e0477f6

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

configure.ac

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ if test "x$CXXFLAGS_overridden" = "xno"; then
203203
AX_CHECK_COMPILE_FLAG([-Wextra],[CXXFLAGS="$CXXFLAGS -Wextra"],,[[$CXXFLAG_WERROR]])
204204
AX_CHECK_COMPILE_FLAG([-Wformat],[CXXFLAGS="$CXXFLAGS -Wformat"],,[[$CXXFLAG_WERROR]])
205205
AX_CHECK_COMPILE_FLAG([-Wformat-security],[CXXFLAGS="$CXXFLAGS -Wformat-security"],,[[$CXXFLAG_WERROR]])
206+
AX_CHECK_COMPILE_FLAG([-Wshadow],[CXXFLAGS="$CXXFLAGS -Wshadow"],,[[$CXXFLAG_WERROR]])
206207

207208
## Some compilers (gcc) ignore unknown -Wno-* options, but warn about all
208209
## unknown options if any other warning is produced. Test the -Wfoo case, and

doc/developer-notes.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,32 @@ Strings and formatting
331331

332332
- *Rationale*: Bitcoin Core uses tinyformat, which is type safe. Leave them out to avoid confusion
333333

334+
Variable names
335+
--------------
336+
337+
The shadowing warning (`-Wshadow`) is enabled by default. It prevents issues rising
338+
from using a different variable with the same name.
339+
340+
Please name variables so that their names do not shadow variables defined in the source code.
341+
342+
E.g. in member initializers, prepend `_` to the argument name shadowing the
343+
member name:
344+
345+
```c++
346+
class AddressBookPage
347+
{
348+
Mode mode;
349+
}
350+
351+
AddressBookPage::AddressBookPage(Mode _mode) :
352+
mode(_mode)
353+
...
354+
```
355+
356+
When using nested cycles, do not name the inner cycle variable the same as in
357+
upper cycle etc.
358+
359+
334360
Threads and synchronization
335361
----------------------------
336362

0 commit comments

Comments
 (0)