use super::{assert_reported, assert_silent_about, codes_of};
use crate::DiagnosticCode;
#[test]
fn a_c_style_cast_to_a_builtin_is_reported() {
let codes = codes_of("void f(double n) {\n int i = (int)n;\n}\n");
assert_reported(
&codes,
DiagnosticCode::CStyleCast,
"`(int)n` names a type in parentheses",
);
}
#[test]
fn a_c_style_cast_to_a_pointer_is_reported() {
let made = codes_of(
"struct Widget { int size; };\nvoid f(void* p) {\n Widget* w = (Widget*)p;\n}\n",
);
assert_reported(&made, DiagnosticCode::CStyleCast, "`(Widget*)p` is a cast");
let without_const = codes_of("void f(const int* p) {\n int* q = (int*)p;\n}\n");
assert_reported(
&without_const,
DiagnosticCode::CStyleCast,
"a C-style cast can drop `const` without saying so",
);
}
#[test]
fn a_cast_to_void_is_silent() {
let codes = codes_of("void f(int n) {\n (void)n;\n}\n");
assert_silent_about(
&codes,
DiagnosticCode::CStyleCast,
"a cast to `void` converts nothing",
);
}
#[test]
fn the_named_casts_are_not_c_style() {
let named = codes_of("void f(double n) {\n int i = static_cast<int>(n);\n}\n");
assert_silent_about(
&named,
DiagnosticCode::CStyleCast,
"`static_cast` says which conversion is meant",
);
let functional = codes_of("void f(double n) {\n int i = int(n);\n}\n");
assert_silent_about(
&functional,
DiagnosticCode::CStyleCast,
"`int(n)` is a functional cast, parsed as a call",
);
}
#[test]
fn parentheses_that_are_not_a_cast_are_silent() {
let parenthesised = codes_of("int g();\nvoid f() {\n int i = (g());\n}\n");
assert_silent_about(
&parenthesised,
DiagnosticCode::CStyleCast,
"`(g())` is a parenthesised call",
);
let arithmetic = codes_of("int g(int a, int b, int c) { return (a + b) * c; }\n");
assert_silent_about(
&arithmetic,
DiagnosticCode::CStyleCast,
"`(a + b)` is an expression, not a type",
);
}