Skip to content

Commit

Permalink
Fix old style cast warning
Browse files Browse the repository at this point in the history
Fixes #2556
  • Loading branch information
keith committed Feb 28, 2024
1 parent ba18304 commit 9195eb0
Show file tree
Hide file tree
Showing 35 changed files with 142 additions and 124 deletions.
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,8 @@ if(OTELCPP_MAINTAINER_MODE)
$<$<STREQUAL:$<COMPILE_LANGUAGE>,CXX>:-Woverloaded-virtual>)
add_compile_options(
$<$<STREQUAL:$<COMPILE_LANGUAGE>,CXX>:-Wsuggest-override>)
add_compile_options(
$<$<STREQUAL:$<COMPILE_LANGUAGE>,CXX>:-Wold-style-cast>)

# C and C++
add_compile_options(-Wcast-qual)
Expand Down Expand Up @@ -527,6 +529,7 @@ if(OTELCPP_MAINTAINER_MODE)
add_compile_options(-Wundef)
add_compile_options(-Wundefined-reinterpret-cast)
add_compile_options(-Wvla)
add_compile_options(-Wold-style-cast)
endif()
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
message("Building with msvc in maintainer mode.")
Expand Down
2 changes: 1 addition & 1 deletion api/include/opentelemetry/trace/span_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class SpanContext final
SpanContext(bool sampled_flag, bool is_remote) noexcept
: trace_id_(),
span_id_(),
trace_flags_(trace::TraceFlags((uint8_t)sampled_flag)),
trace_flags_(trace::TraceFlags(static_cast<uint8_t>(sampled_flag))),
is_remote_(is_remote),
trace_state_(TraceState::GetDefault())
{}
Expand Down
37 changes: 20 additions & 17 deletions api/test/context/context_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ using namespace opentelemetry;
// Tests that the context constructor accepts an std::map.
TEST(ContextTest, ContextIterableAcceptsMap)
{
std::map<std::string, context::ContextValue> map_test = {{"test_key", (int64_t)123}};
std::map<std::string, context::ContextValue> map_test = {{"test_key", static_cast<int64_t>(123)}};
context::Context test_context = context::Context(map_test);
}

// Tests that the GetValue method returns the expected value.
TEST(ContextTest, ContextGetValueReturnsExpectedValue)
{
std::map<std::string, context::ContextValue> map_test = {{"test_key", (int64_t)123},
{"foo_key", (int64_t)456}};
std::map<std::string, context::ContextValue> map_test = {{"test_key", static_cast<int64_t>(123)},
{"foo_key", static_cast<int64_t>(456)}};
const context::Context test_context = context::Context(map_test);
EXPECT_EQ(nostd::get<int64_t>(test_context.GetValue("test_key")), 123);
EXPECT_EQ(nostd::get<int64_t>(test_context.GetValue("foo_key")), 456);
Expand All @@ -29,8 +29,9 @@ TEST(ContextTest, ContextGetValueReturnsExpectedValue)
// Tests that the SetValues method accepts an std::map.
TEST(ContextTest, ContextSetValuesAcceptsMap)
{
std::map<std::string, context::ContextValue> map_test = {{"test_key", (int64_t)123}};
std::map<std::string, context::ContextValue> map_test_write = {{"foo_key", (int64_t)456}};
std::map<std::string, context::ContextValue> map_test = {{"test_key", static_cast<int64_t>(123)}};
std::map<std::string, context::ContextValue> map_test_write = {
{"foo_key", static_cast<int64_t>(456)}};

context::Context test_context = context::Context(map_test);
context::Context foo_context = test_context.SetValues(map_test_write);
Expand All @@ -44,7 +45,7 @@ TEST(ContextTest, ContextSetValuesAcceptsMap)
TEST(ContextTest, ContextSetValuesAcceptsStringViewContextValue)
{
nostd::string_view string_view_test = "string_view";
context::ContextValue context_value_test = (int64_t)123;
context::ContextValue context_value_test = static_cast<int64_t>(123);

context::Context test_context = context::Context(string_view_test, context_value_test);
context::Context foo_context = test_context.SetValue(string_view_test, context_value_test);
Expand All @@ -56,21 +57,21 @@ TEST(ContextTest, ContextSetValuesAcceptsStringViewContextValue)
// written to it.
TEST(ContextTest, ContextImmutability)
{
std::map<std::string, context::ContextValue> map_test = {{"test_key", (int64_t)123}};
std::map<std::string, context::ContextValue> map_test = {{"test_key", static_cast<int64_t>(123)}};

context::Context context_test = context::Context(map_test);
context::Context context_foo = context_test.SetValue("foo_key", (int64_t)456);
context::Context context_foo = context_test.SetValue("foo_key", static_cast<int64_t>(456));

EXPECT_FALSE(nostd::holds_alternative<int64_t>(context_test.GetValue("foo_key")));
}

// Tests that writing the same to a context overwrites the original value.
TEST(ContextTest, ContextKeyOverwrite)
{
std::map<std::string, context::ContextValue> map_test = {{"test_key", (int64_t)123}};
std::map<std::string, context::ContextValue> map_test = {{"test_key", static_cast<int64_t>(123)}};

context::Context context_test = context::Context(map_test);
context::Context context_foo = context_test.SetValue("test_key", (int64_t)456);
context::Context context_foo = context_test.SetValue("test_key", static_cast<int64_t>(456));

EXPECT_EQ(nostd::get<int64_t>(context_foo.GetValue("test_key")), 456);
}
Expand All @@ -81,8 +82,8 @@ TEST(ContextTest, ContextInheritance)
{
using M = std::map<std::string, context::ContextValue>;

M m1 = {{"test_key", (int64_t)123}, {"foo_key", (int64_t)321}};
M m2 = {{"other_key", (int64_t)789}, {"another_key", (int64_t)987}};
M m1 = {{"test_key", static_cast<int64_t>(123)}, {"foo_key", static_cast<int64_t>(321)}};
M m2 = {{"other_key", static_cast<int64_t>(789)}, {"another_key", static_cast<int64_t>(987)}};

context::Context test_context = context::Context(m1);
context::Context foo_context = test_context.SetValues(m2);
Expand All @@ -100,7 +101,9 @@ TEST(ContextTest, ContextInheritance)
TEST(ContextTest, ContextCopyOperator)
{
std::map<std::string, context::ContextValue> test_map = {
{"test_key", (int64_t)123}, {"foo_key", (int64_t)456}, {"other_key", (int64_t)789}};
{"test_key", static_cast<int64_t>(123)},
{"foo_key", static_cast<int64_t>(456)},
{"other_key", static_cast<int64_t>(789)}};

context::Context test_context = context::Context(test_map);
context::Context copied_context = test_context;
Expand All @@ -121,7 +124,7 @@ TEST(ContextTest, ContextEmptyMap)
// false if not.
TEST(ContextTest, ContextHasKey)
{
std::map<std::string, context::ContextValue> map_test = {{"test_key", (int64_t)123}};
std::map<std::string, context::ContextValue> map_test = {{"test_key", static_cast<int64_t>(123)}};
const context::Context context_test = context::Context(map_test);
EXPECT_TRUE(context_test.HasKey("test_key"));
EXPECT_FALSE(context_test.HasKey("foo_key"));
Expand All @@ -130,7 +133,7 @@ TEST(ContextTest, ContextHasKey)
// Tests that a copied context returns true when compared
TEST(ContextTest, ContextCopyCompare)
{
std::map<std::string, context::ContextValue> map_test = {{"test_key", (int64_t)123}};
std::map<std::string, context::ContextValue> map_test = {{"test_key", static_cast<int64_t>(123)}};
context::Context context_test = context::Context(map_test);
context::Context copied_test = context_test;
EXPECT_TRUE(context_test == copied_test);
Expand All @@ -139,8 +142,8 @@ TEST(ContextTest, ContextCopyCompare)
// Tests that two differently constructed contexts return false when compared
TEST(ContextTest, ContextDiffCompare)
{
std::map<std::string, context::ContextValue> map_test = {{"test_key", (int64_t)123}};
std::map<std::string, context::ContextValue> map_foo = {{"foo_key", (int64_t)123}};
std::map<std::string, context::ContextValue> map_test = {{"test_key", static_cast<int64_t>(123)}};
std::map<std::string, context::ContextValue> map_foo = {{"foo_key", static_cast<int64_t>(123)}};
context::Context context_test = context::Context(map_test);
context::Context foo_test = context::Context(map_foo);
EXPECT_FALSE(context_test == foo_test);
Expand Down
25 changes: 13 additions & 12 deletions api/test/context/runtime_context_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ using namespace opentelemetry;
// Tests that GetCurrent returns the current context
TEST(RuntimeContextTest, GetCurrent)
{
std::map<std::string, context::ContextValue> map_test = {{"test_key", (int64_t)123}};
std::map<std::string, context::ContextValue> map_test = {{"test_key", static_cast<int64_t>(123)}};
context::Context test_context = context::Context(map_test);
auto old_context = context::RuntimeContext::Attach(test_context);
EXPECT_EQ(context::RuntimeContext::GetCurrent(), test_context);
Expand All @@ -23,7 +23,7 @@ TEST(RuntimeContextTest, GetCurrent)
// Tests that detach resets the context to the previous context
TEST(RuntimeContextTest, Detach)
{
std::map<std::string, context::ContextValue> map_test = {{"test_key", (int64_t)123}};
std::map<std::string, context::ContextValue> map_test = {{"test_key", static_cast<int64_t>(123)}};
context::Context test_context = context::Context(map_test);
context::Context foo_context = context::Context(map_test);

Expand All @@ -38,7 +38,7 @@ TEST(RuntimeContextTest, Detach)
// Tests that detach returns false when the wrong context is provided
TEST(RuntimeContextTest, DetachWrongContext)
{
std::map<std::string, context::ContextValue> map_test = {{"test_key", (int64_t)123}};
std::map<std::string, context::ContextValue> map_test = {{"test_key", static_cast<int64_t>(123)}};
context::Context test_context = context::Context(map_test);
auto test_context_token = context::RuntimeContext::Attach(test_context);
EXPECT_TRUE(context::RuntimeContext::Detach(*test_context_token));
Expand All @@ -48,7 +48,7 @@ TEST(RuntimeContextTest, DetachWrongContext)
// Tests that the ThreadLocalContext can handle three attached contexts
TEST(RuntimeContextTest, ThreeAttachDetach)
{
std::map<std::string, context::ContextValue> map_test = {{"test_key", (int64_t)123}};
std::map<std::string, context::ContextValue> map_test = {{"test_key", static_cast<int64_t>(123)}};
context::Context test_context = context::Context(map_test);
context::Context foo_context = context::Context(map_test);
context::Context other_context = context::Context(map_test);
Expand All @@ -66,9 +66,10 @@ TEST(RuntimeContextTest, ThreeAttachDetach)
// RuntimeContext::SetValue method.
TEST(RuntimeContextTest, SetValueRuntimeContext)
{
context::Context foo_context = context::Context("foo_key", (int64_t)596);
auto old_context_token = context::RuntimeContext::Attach(foo_context);
context::Context test_context = context::RuntimeContext::SetValue("test_key", (int64_t)123);
context::Context foo_context = context::Context("foo_key", static_cast<int64_t>(596));
auto old_context_token = context::RuntimeContext::Attach(foo_context);
context::Context test_context =
context::RuntimeContext::SetValue("test_key", static_cast<int64_t>(123));
EXPECT_EQ(nostd::get<int64_t>(test_context.GetValue("test_key")), 123);
EXPECT_EQ(nostd::get<int64_t>(test_context.GetValue("foo_key")), 596);
}
Expand All @@ -78,9 +79,9 @@ TEST(RuntimeContextTest, SetValueRuntimeContext)
// RuntimeContext::SetValue method.
TEST(RuntimeContextTest, SetValueOtherContext)
{
context::Context foo_context = context::Context("foo_key", (int64_t)596);
context::Context foo_context = context::Context("foo_key", static_cast<int64_t>(596));
context::Context test_context =
context::RuntimeContext::SetValue("test_key", (int64_t)123, &foo_context);
context::RuntimeContext::SetValue("test_key", static_cast<int64_t>(123), &foo_context);
EXPECT_EQ(nostd::get<int64_t>(test_context.GetValue("test_key")), 123);
EXPECT_EQ(nostd::get<int64_t>(test_context.GetValue("foo_key")), 596);
}
Expand All @@ -89,7 +90,7 @@ TEST(RuntimeContextTest, SetValueOtherContext)
// passed in string and the current Runtime Context
TEST(RuntimeContextTest, GetValueRuntimeContext)
{
context::Context foo_context = context::Context("foo_key", (int64_t)596);
context::Context foo_context = context::Context("foo_key", static_cast<int64_t>(596));
auto old_context_token = context::RuntimeContext::Attach(foo_context);
EXPECT_EQ(nostd::get<int64_t>(context::RuntimeContext::GetValue("foo_key")), 596);
}
Expand All @@ -98,7 +99,7 @@ TEST(RuntimeContextTest, GetValueRuntimeContext)
// passed in string and the passed in context
TEST(RuntimeContextTest, GetValueOtherContext)
{
context::Context foo_context = context::Context("foo_key", (int64_t)596);
context::Context foo_context = context::Context("foo_key", static_cast<int64_t>(596));
EXPECT_EQ(nostd::get<int64_t>(context::RuntimeContext::GetValue("foo_key", &foo_context)), 596);
}

Expand All @@ -114,7 +115,7 @@ TEST(RuntimeContextTest, DetachOutOfOrder)
std::vector<context::Context> contexts;
for (auto i : indices)
{
contexts.push_back(context::Context("index", (int64_t)i));
contexts.push_back(context::Context("index", static_cast<int64_t>(i)));
}

do
Expand Down
4 changes: 2 additions & 2 deletions api/test/nostd/span_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,10 @@ TEST(SpanTest, Iteration)
std::array<int, 3> array = {1, 2, 3};

span<int> s1{array.data(), array.size()};
EXPECT_EQ(std::distance(s1.begin(), s1.end()), (ptrdiff_t)array.size());
EXPECT_EQ(std::distance(s1.begin(), s1.end()), static_cast<ptrdiff_t>(array.size()));
EXPECT_TRUE(std::equal(s1.begin(), s1.end(), array.begin()));

span<int, 3> s2{array.data(), array.size()};
EXPECT_EQ(std::distance(s2.begin(), s2.end()), (ptrdiff_t)array.size());
EXPECT_EQ(std::distance(s2.begin(), s2.end()), static_cast<ptrdiff_t>(array.size()));
EXPECT_TRUE(std::equal(s2.begin(), s2.end(), array.begin()));
}
4 changes: 2 additions & 2 deletions api/test/singleton/singleton_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ void do_something()
void *component_g = dlopen("libcomponent_g.so", RTLD_NOW);
EXPECT_NE(component_g, nullptr);

auto *func_g = (void (*)())dlsym(component_g, "do_something_in_g");
auto *func_g = reinterpret_cast<void (*)()>(dlsym(component_g, "do_something_in_g"));
EXPECT_NE(func_g, nullptr);

(*func_g)();
Expand All @@ -67,7 +67,7 @@ void do_something()
void *component_h = dlopen("libcomponent_h.so", RTLD_NOW);
EXPECT_NE(component_h, nullptr);

auto *func_h = (void (*)())dlsym(component_h, "do_something_in_h");
auto *func_h = reinterpret_cast<void (*)()>(dlsym(component_h, "do_something_in_h"));
EXPECT_NE(func_h, nullptr);

(*func_h)();
Expand Down
3 changes: 2 additions & 1 deletion examples/batch/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ void InitTracer()
// We export `kNumSpans` after every `schedule_delay_millis` milliseconds.
options.max_export_batch_size = kNumSpans;

resource::ResourceAttributes attributes = {{"service", "test_service"}, {"version", (uint32_t)1}};
resource::ResourceAttributes attributes = {{"service", "test_service"},
{"version", static_cast<uint32_t>(1)}};
auto resource = resource::Resource::Create(attributes);

auto processor = trace_sdk::BatchSpanProcessorFactory::Create(std::move(exporter), options);
Expand Down
2 changes: 1 addition & 1 deletion examples/http/client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ int main(int argc, char *argv[])
// The port the validation service listens to can be specified via the command line.
if (argc > 1)
{
port = (uint16_t)(atoi(argv[1]));
port = static_cast<uint16_t>(atoi(argv[1]));
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion examples/http/server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ int main(int argc, char *argv[])
// The port the validation service listens to can be specified via the command line.
if (argc > 1)
{
server_port = (uint16_t)atoi(argv[1]);
server_port = static_cast<uint16_t>(atoi(argv[1]));
}

HttpServer http_server(server_name, server_port);
Expand Down
4 changes: 2 additions & 2 deletions exporters/ostream/test/ostream_metric_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ TEST(OStreamMetricsExporter, ExportHistogramPointData)
histogram_point_data2.boundaries_ = std::vector<double>{10.0, 20.0, 30.0};
histogram_point_data2.count_ = 3;
histogram_point_data2.counts_ = {200, 300, 400, 500};
histogram_point_data2.sum_ = (int64_t)900;
histogram_point_data2.sum_ = static_cast<int64_t>(900);
metric_sdk::ResourceMetrics data;
auto resource = opentelemetry::sdk::resource::Resource::Create(
opentelemetry::sdk::resource::ResourceAttributes{});
Expand Down Expand Up @@ -191,7 +191,7 @@ TEST(OStreamMetricsExporter, ExportLastValuePointData)
last_value_point_data.is_lastvalue_valid_ = true;
last_value_point_data.sample_ts_ = opentelemetry::common::SystemTimestamp{};
metric_sdk::LastValuePointData last_value_point_data2{};
last_value_point_data2.value_ = (int64_t)20;
last_value_point_data2.value_ = static_cast<int64_t>(20);
last_value_point_data2.is_lastvalue_valid_ = true;
last_value_point_data2.sample_ts_ = opentelemetry::common::SystemTimestamp{};
metric_sdk::MetricData metric_data{
Expand Down
8 changes: 4 additions & 4 deletions exporters/otlp/test/otlp_http_metric_exporter_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ class OtlpHttpMetricExporterTestPeer : public ::testing::Test
last_value_point_data.is_lastvalue_valid_ = true;
last_value_point_data.sample_ts_ = opentelemetry::common::SystemTimestamp{};
opentelemetry::sdk::metrics::LastValuePointData last_value_point_data2{};
last_value_point_data2.value_ = (int64_t)20;
last_value_point_data2.value_ = static_cast<int64_t>(20);
last_value_point_data2.is_lastvalue_valid_ = true;
last_value_point_data2.sample_ts_ = opentelemetry::common::SystemTimestamp{};
opentelemetry::sdk::metrics::MetricData metric_data{
Expand Down Expand Up @@ -404,7 +404,7 @@ class OtlpHttpMetricExporterTestPeer : public ::testing::Test
last_value_point_data.is_lastvalue_valid_ = true;
last_value_point_data.sample_ts_ = opentelemetry::common::SystemTimestamp{};
opentelemetry::sdk::metrics::LastValuePointData last_value_point_data2{};
last_value_point_data2.value_ = (int64_t)20;
last_value_point_data2.value_ = static_cast<int64_t>(20);
last_value_point_data2.is_lastvalue_valid_ = true;
last_value_point_data2.sample_ts_ = opentelemetry::common::SystemTimestamp{};
opentelemetry::sdk::metrics::MetricData metric_data{
Expand Down Expand Up @@ -504,7 +504,7 @@ class OtlpHttpMetricExporterTestPeer : public ::testing::Test
histogram_point_data2.boundaries_ = {10.0, 20.0, 30.0};
histogram_point_data2.count_ = 3;
histogram_point_data2.counts_ = {200, 300, 400, 500};
histogram_point_data2.sum_ = (int64_t)900;
histogram_point_data2.sum_ = static_cast<int64_t>(900);

opentelemetry::sdk::metrics::MetricData metric_data{
opentelemetry::sdk::metrics::InstrumentDescriptor{
Expand Down Expand Up @@ -639,7 +639,7 @@ class OtlpHttpMetricExporterTestPeer : public ::testing::Test
histogram_point_data2.boundaries_ = {10.0, 20.0, 30.0};
histogram_point_data2.count_ = 3;
histogram_point_data2.counts_ = {200, 300, 400, 500};
histogram_point_data2.sum_ = (int64_t)900;
histogram_point_data2.sum_ = static_cast<int64_t>(900);

opentelemetry::sdk::metrics::MetricData metric_data{
opentelemetry::sdk::metrics::InstrumentDescriptor{
Expand Down
9 changes: 5 additions & 4 deletions exporters/prometheus/src/exporter_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ inline std::string Sanitize(std::string name, const T &valid)
constexpr const auto replacement_dup = '=';

bool has_dup = false;
for (int i = 0; i < (int)name.size(); ++i)
for (int i = 0; i < static_cast<int>(name.size()); ++i)
{
if (valid(i, name[i]) && name[i] != replacement)
{
Expand Down Expand Up @@ -172,8 +172,9 @@ std::vector<prometheus_client::MetricFamily> PrometheusExporterUtils::TranslateT
{
sum = static_cast<double>(nostd::get<int64_t>(histogram_point_data.sum_));
}
SetData(std::vector<double>{sum, (double)histogram_point_data.count_}, boundaries, counts,
point_data_attr.attributes, scope, time, &metric_family, data.resource_);
SetData(std::vector<double>{sum, static_cast<double>(histogram_point_data.count_)},
boundaries, counts, point_data_attr.attributes, scope, time, &metric_family,
data.resource_);
}
else if (type == prometheus_client::MetricType::Gauge)
{
Expand Down Expand Up @@ -258,7 +259,7 @@ std::string PrometheusExporterUtils::SanitizeNames(std::string name)
};

bool has_dup = false;
for (int i = 0; i < (int)name.size(); ++i)
for (int i = 0; i < static_cast<int>(name.size()); ++i)
{
if (valid(i, name[i]))
{
Expand Down
Loading

0 comments on commit 9195eb0

Please sign in to comment.