use super::{assert_reported, assert_silent_about, codes_of};
use crate::DiagnosticCode;
#[test]
fn a_view_member_of_a_by_value_parameter_is_reported() {
let codes = codes_of(
"struct string {};\nstruct string_view {};\nstruct S {\n string_view v;\n S(string s) : v(s) {}\n};\n",
);
assert_reported(
&codes,
DiagnosticCode::DanglingField,
"`s` is a parameter that dies when the constructor returns",
);
}
#[test]
fn a_view_member_of_a_temporary_is_reported() {
let codes = codes_of(
"struct string {};\nstruct string_view {};\nstruct S {\n string_view v = string(\"x\");\n};\n",
);
assert_reported(
&codes,
DiagnosticCode::DanglingField,
"`string(\"x\")` is a temporary",
);
}
#[test]
fn a_reference_member_of_a_temporary_is_reported() {
let codes = codes_of(
"struct string {};\nstruct S {\n const string& v;\n S() : v(string(\"x\")) {}\n};\n",
);
assert_reported(
&codes,
DiagnosticCode::DanglingField,
"the member refers to a temporary",
);
}
#[test]
fn a_pointer_member_of_a_parameters_buffer_is_reported() {
let codes = codes_of(
"struct string {};\nstruct S {\n const char* p;\n S(string s) : p(s.c_str()) {}\n};\n",
);
assert_reported(
&codes,
DiagnosticCode::DanglingField,
"the pointer refers into `s`",
);
}
#[test]
fn an_owning_member_a_reference_parameter_and_this_are_silent() {
let owning =
codes_of("struct string {};\nstruct S {\n string v;\n S(string s) : v(s) {}\n};\n");
assert_silent_about(
&owning,
DiagnosticCode::DanglingField,
"`v` owns its copy, so there is nothing to dangle",
);
let reference_parameter = codes_of(
"struct string {};\nstruct string_view {};\nstruct S {\n string_view v;\n S(const string& s) : v(s) {}\n};\n",
);
assert_silent_about(
&reference_parameter,
DiagnosticCode::DanglingField,
"the caller owns what `s` refers to",
);
let member_of_this = codes_of(
"struct string {};\nstruct string_view {};\nstruct S {\n string s;\n string_view v;\n S() : v(s) {}\n};\n",
);
assert_silent_about(
&member_of_this,
DiagnosticCode::DanglingField,
"`s` is a member and outlives the view of it",
);
}
#[test]
fn a_string_literal_and_an_unknown_pointer_are_silent() {
let literal =
codes_of("struct string_view {};\nstruct S {\n string_view v = \"text\";\n};\n");
assert_silent_about(
&literal,
DiagnosticCode::DanglingField,
"the array a literal names outlives the object",
);
let unknown = codes_of("struct S {\n int* p;\n S(int* q) : p(q) {}\n};\n");
assert_silent_about(
&unknown,
DiagnosticCode::DanglingField,
"`q` may point at anything, and the rule does not guess",
);
}
#[test]
fn a_view_member_of_a_view_parameter_is_silent() {
let view = codes_of(
"struct string_view {};\nstruct Word {\n string_view Item;\n Word(string_view item) : Item(item) {}\n};\n",
);
assert_silent_about(
&view,
DiagnosticCode::DanglingField,
"a view parameter names the caller's bytes; the member copies the view, not the parameter",
);
let pointer = codes_of(
"struct string_view {};\nstruct S {\n string_view v;\n S(const char *p) : v(p) {}\n};\n",
);
assert_silent_about(
&pointer,
DiagnosticCode::DanglingField,
"the characters a pointer parameter names are the caller's",
);
}