Skip to main content

Module redefinition

Module redefinition 

Source
Expand description

redefinition: a name declared twice in one scope.

void f() {
    int count = 1;
    int count = 2;   // `count` is already declared here
}

§One scope, and only the kinds that cannot be repeated

Two declarations of one name in one scope is a mistake when the language has no reading for it: an overload set is exactly the reading for functions, so two void run(); are two overloads and nothing is reported, while two variables of one name cannot both exist. The kinds this judges are therefore the ones with no second reading — see [a_kind_that_cannot_be_repeated] — and the scopes are the ones a reader writes: a function body, a block, a lambda.

§Why not a class body, and why not a file

  class body    `class A;` then `class A { … };` is a forward declaration and its definition — one entity
                written twice, which the scope tree records twice. Judging that as a redefinition would be a
                false positive on the commonest declaration in C++.
  file scope    `extern int value;` then `int value = 0;` is the same entity too, and legal.
  a macro       a binding that came from a macro's expansion has a range inside the macro — a finding about it
                would point at code the reader wrote once

§What it caught

test_cpp/main.cpp writes auto x = 123; on line 22 and auto x = anotherVec[1]; on line 39, in one function — a program no compiler accepts, and one this analysis reported nothing about until this rule.

Structs§

Redefinition
redefinition — see the module documentation.