use super::{assert_reported, assert_silent_about, codes_of};
use crate::DiagnosticCode;
#[test]
fn a_local_declared_twice_in_one_scope_is_reported() {
let twice = codes_of("void f() {\n int count = 1;\n int count = 2;\n}\n");
assert_reported(
&twice,
DiagnosticCode::Redefinition,
"`count` is declared twice in the function's own scope",
);
let auto_twice = codes_of("void f() {\n auto x = 123;\n auto x = 0;\n}\n");
assert_reported(
&auto_twice,
DiagnosticCode::Redefinition,
"two `auto` declarations of `x` are two variables with one name",
);
}
#[test]
fn a_parameter_and_a_local_share_the_function_scope() {
let shadows_a_parameter = codes_of("void f(int count) {\n int count = 2;\n}\n");
assert_reported(
&shadows_a_parameter,
DiagnosticCode::Redefinition,
"the parameter and the local `count` are both declared in `f`'s scope",
);
}
#[test]
fn a_repeat_the_language_has_a_reading_for_is_quiet() {
let overloads = codes_of("void run();\nvoid run(int value);\n");
assert_silent_about(
&overloads,
DiagnosticCode::Redefinition,
"two functions of one name are two overloads, which is the language's own reading",
);
let forward_and_definition = codes_of("class Widget;\nclass Widget { int size; };\n");
assert_silent_about(
&forward_and_definition,
DiagnosticCode::Redefinition,
"`class Widget;` and its definition are one entity written twice",
);
let extern_and_definition = codes_of("extern int value;\nint value = 0;\n");
assert_silent_about(
&extern_and_definition,
DiagnosticCode::Redefinition,
"`extern int value;` promises the same entity the definition provides",
);
}
#[test]
fn a_repeat_in_another_scope_is_a_shadow() {
let two_blocks = codes_of("void f() {\n { int count = 1; }\n { int count = 2; }\n}\n");
assert_silent_about(
&two_blocks,
DiagnosticCode::Redefinition,
"the two `count`s are in two sibling blocks, which are two scopes",
);
let shadowing = codes_of("void f() {\n int count = 1;\n { int count = 2; }\n}\n");
assert_silent_about(
&shadowing,
DiagnosticCode::Redefinition,
"the inner `count` is a different scope's declaration, which C++ lets shadow",
);
let a_class_body = codes_of("struct S {\n int size;\n int size;\n};\n");
assert_silent_about(
&a_class_body,
DiagnosticCode::Redefinition,
"the rule judges function bodies, blocks and lambdas, and a class body is none of them",
);
}