From 3a1194be40ee18fc1895f36ddd19691b1be7c4ba Mon Sep 17 00:00:00 2001 From: Tyler Veness Date: Wed, 8 Nov 2023 12:47:23 -0800 Subject: [PATCH] Replace static_cast() with [[maybe_unused]] attribute (#5892) This clarifies intent. Not done for thirdparty libraries or structured binding variables. --- .../src/main/native/include/LifetimeWrappers.h | 12 ++++++------ .../src/test/native/cpp/StateSpaceUtilTest.cpp | 7 +++---- .../cpp/estimator/ExtendedKalmanFilterTest.cpp | 10 ++++------ .../cpp/estimator/UnscentedKalmanFilterTest.cpp | 10 ++++------ .../native/cpp/spline/CubicHermiteSplineTest.cpp | 12 ------------ .../cpp/spline/QuinticHermiteSplineTest.cpp | 12 ------------ wpiutil/src/test/native/cpp/ArrayTest.cpp | 15 ++++++--------- 7 files changed, 23 insertions(+), 55 deletions(-) diff --git a/crossConnIntegrationTests/src/main/native/include/LifetimeWrappers.h b/crossConnIntegrationTests/src/main/native/include/LifetimeWrappers.h index 57312005d1..e196d84e77 100644 --- a/crossConnIntegrationTests/src/main/native/include/LifetimeWrappers.h +++ b/crossConnIntegrationTests/src/main/native/include/LifetimeWrappers.h @@ -190,12 +190,12 @@ struct RelayHandle { HAL_RelayHandle handle = 0; }; -#define ASSERT_LAST_ERROR_STATUS(status, x) \ - do { \ - ASSERT_EQ(status, HAL_USE_LAST_ERROR); \ - const char* lastErrorMessageInMacro = HAL_GetLastError(&status); \ - static_cast(lastErrorMessageInMacro); \ - ASSERT_EQ(status, x); \ +#define ASSERT_LAST_ERROR_STATUS(status, x) \ + do { \ + ASSERT_EQ(status, HAL_USE_LAST_ERROR); \ + [[maybe_unused]] const char* lastErrorMessageInMacro = \ + HAL_GetLastError(&status); \ + ASSERT_EQ(status, x); \ } while (0) } // namespace hlt diff --git a/wpimath/src/test/native/cpp/StateSpaceUtilTest.cpp b/wpimath/src/test/native/cpp/StateSpaceUtilTest.cpp index 758dc3034f..4de13d3684 100644 --- a/wpimath/src/test/native/cpp/StateSpaceUtilTest.cpp +++ b/wpimath/src/test/native/cpp/StateSpaceUtilTest.cpp @@ -63,13 +63,12 @@ TEST(StateSpaceUtilTest, CovArray) { } TEST(StateSpaceUtilTest, WhiteNoiseVectorParameterPack) { - frc::Vectord<2> vec = frc::MakeWhiteNoiseVector(2.0, 3.0); - static_cast(vec); + [[maybe_unused]] frc::Vectord<2> vec = frc::MakeWhiteNoiseVector(2.0, 3.0); } TEST(StateSpaceUtilTest, WhiteNoiseVectorArray) { - frc::Vectord<2> vec = frc::MakeWhiteNoiseVector<2>({2.0, 3.0}); - static_cast(vec); + [[maybe_unused]] frc::Vectord<2> vec = + frc::MakeWhiteNoiseVector<2>({2.0, 3.0}); } TEST(StateSpaceUtilTest, IsStabilizable) { diff --git a/wpimath/src/test/native/cpp/estimator/ExtendedKalmanFilterTest.cpp b/wpimath/src/test/native/cpp/estimator/ExtendedKalmanFilterTest.cpp index 2a5ce800cb..d631cfc7f8 100644 --- a/wpimath/src/test/native/cpp/estimator/ExtendedKalmanFilterTest.cpp +++ b/wpimath/src/test/native/cpp/estimator/ExtendedKalmanFilterTest.cpp @@ -49,15 +49,13 @@ frc::Vectord<5> Dynamics(const frc::Vectord<5>& x, const frc::Vectord<2>& u) { k1.value() * ((C1 * vr).value() + (C2 * Vr).value())}; } -frc::Vectord<3> LocalMeasurementModel(const frc::Vectord<5>& x, - const frc::Vectord<2>& u) { - static_cast(u); +frc::Vectord<3> LocalMeasurementModel( + const frc::Vectord<5>& x, [[maybe_unused]] const frc::Vectord<2>& u) { return frc::Vectord<3>{x(2), x(3), x(4)}; } -frc::Vectord<5> GlobalMeasurementModel(const frc::Vectord<5>& x, - const frc::Vectord<2>& u) { - static_cast(u); +frc::Vectord<5> GlobalMeasurementModel( + const frc::Vectord<5>& x, [[maybe_unused]] const frc::Vectord<2>& u) { return frc::Vectord<5>{x(0), x(1), x(2), x(3), x(4)}; } } // namespace diff --git a/wpimath/src/test/native/cpp/estimator/UnscentedKalmanFilterTest.cpp b/wpimath/src/test/native/cpp/estimator/UnscentedKalmanFilterTest.cpp index 0f97a88008..66be508309 100644 --- a/wpimath/src/test/native/cpp/estimator/UnscentedKalmanFilterTest.cpp +++ b/wpimath/src/test/native/cpp/estimator/UnscentedKalmanFilterTest.cpp @@ -51,15 +51,13 @@ frc::Vectord<5> Dynamics(const frc::Vectord<5>& x, const frc::Vectord<2>& u) { k1.value() * ((C1 * vr).value() + (C2 * Vr).value())}; } -frc::Vectord<3> LocalMeasurementModel(const frc::Vectord<5>& x, - const frc::Vectord<2>& u) { - static_cast(u); +frc::Vectord<3> LocalMeasurementModel( + const frc::Vectord<5>& x, [[maybe_unused]] const frc::Vectord<2>& u) { return frc::Vectord<3>{x(2), x(3), x(4)}; } -frc::Vectord<5> GlobalMeasurementModel(const frc::Vectord<5>& x, - const frc::Vectord<2>& u) { - static_cast(u); +frc::Vectord<5> GlobalMeasurementModel( + const frc::Vectord<5>& x, [[maybe_unused]] const frc::Vectord<2>& u) { return frc::Vectord<5>{x(0), x(1), x(2), x(3), x(4)}; } } // namespace diff --git a/wpimath/src/test/native/cpp/spline/CubicHermiteSplineTest.cpp b/wpimath/src/test/native/cpp/spline/CubicHermiteSplineTest.cpp index c98d1908ec..3a4acc0e65 100644 --- a/wpimath/src/test/native/cpp/spline/CubicHermiteSplineTest.cpp +++ b/wpimath/src/test/native/cpp/spline/CubicHermiteSplineTest.cpp @@ -21,9 +21,6 @@ class CubicHermiteSplineTest : public ::testing::Test { protected: static void Run(const Pose2d& a, const std::vector& waypoints, const Pose2d& b) { - // Start the timer. - const auto start = std::chrono::high_resolution_clock::now(); - // Generate and parameterize the spline. const auto [startCV, endCV] = @@ -40,13 +37,6 @@ class CubicHermiteSplineTest : public ::testing::Test { poses.insert(std::end(poses), std::begin(x) + 1, std::end(x)); } - // End timer. - const auto finish = std::chrono::high_resolution_clock::now(); - - // Calculate the duration (used when benchmarking) - const auto duration = - std::chrono::duration_cast(finish - start); - for (unsigned int i = 0; i < poses.size() - 1; i++) { auto& p0 = poses[i]; auto& p1 = poses[i + 1]; @@ -87,8 +77,6 @@ class CubicHermiteSplineTest : public ::testing::Test { EXPECT_NEAR(poses.back().first.Y().value(), b.Y().value(), 1E-9); EXPECT_NEAR(poses.back().first.Rotation().Radians().value(), b.Rotation().Radians().value(), 1E-9); - - static_cast(duration); } }; } // namespace frc diff --git a/wpimath/src/test/native/cpp/spline/QuinticHermiteSplineTest.cpp b/wpimath/src/test/native/cpp/spline/QuinticHermiteSplineTest.cpp index a0df9bb687..5f80f3ad51 100644 --- a/wpimath/src/test/native/cpp/spline/QuinticHermiteSplineTest.cpp +++ b/wpimath/src/test/native/cpp/spline/QuinticHermiteSplineTest.cpp @@ -20,20 +20,10 @@ namespace frc { class QuinticHermiteSplineTest : public ::testing::Test { protected: static void Run(const Pose2d& a, const Pose2d& b) { - // Start the timer. - const auto start = std::chrono::high_resolution_clock::now(); - // Generate and parameterize the spline. const auto spline = SplineHelper::QuinticSplinesFromWaypoints({a, b})[0]; const auto poses = SplineParameterizer::Parameterize(spline); - // End timer. - const auto finish = std::chrono::high_resolution_clock::now(); - - // Calculate the duration (used when benchmarking) - const auto duration = - std::chrono::duration_cast(finish - start); - for (unsigned int i = 0; i < poses.size() - 1; i++) { auto& p0 = poses[i]; auto& p1 = poses[i + 1]; @@ -59,8 +49,6 @@ class QuinticHermiteSplineTest : public ::testing::Test { EXPECT_NEAR(poses.back().first.Y().value(), b.Y().value(), 1E-9); EXPECT_NEAR(poses.back().first.Rotation().Radians().value(), b.Rotation().Radians().value(), 1E-9); - - static_cast(duration); } }; } // namespace frc diff --git a/wpiutil/src/test/native/cpp/ArrayTest.cpp b/wpiutil/src/test/native/cpp/ArrayTest.cpp index f70b201f42..d3a12a4bb9 100644 --- a/wpiutil/src/test/native/cpp/ArrayTest.cpp +++ b/wpiutil/src/test/native/cpp/ArrayTest.cpp @@ -16,20 +16,17 @@ class MoveOnlyType { } // namespace TEST(ArrayTest, CopyableTypeCompiles) { - constexpr wpi::array arr1{1, 2, 3}; - static_cast(arr1); + [[maybe_unused]] constexpr wpi::array arr1{1, 2, 3}; // Test deduction guide - constexpr wpi::array arr2{1, 2, 3}; - static_cast(arr2); + [[maybe_unused]] constexpr wpi::array arr2{1, 2, 3}; } TEST(ArrayTest, MoveOnlyTypeCompiles) { - constexpr wpi::array arr1{MoveOnlyType{}, MoveOnlyType{}, - MoveOnlyType{}}; - static_cast(arr1); + [[maybe_unused]] constexpr wpi::array arr1{ + MoveOnlyType{}, MoveOnlyType{}, MoveOnlyType{}}; // Test deduction guide - constexpr wpi::array arr2{MoveOnlyType{}, MoveOnlyType{}, MoveOnlyType{}}; - static_cast(arr2); + [[maybe_unused]] constexpr wpi::array arr2{MoveOnlyType{}, MoveOnlyType{}, + MoveOnlyType{}}; }