Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix or suppress lint in anticipation of cpplint upgrade #22722

Merged
merged 19 commits into from
Mar 10, 2025
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion common/hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,14 @@ namespace drake {
template <class HashAlgorithm, class T>
std::enable_if_t<std::is_integral_v<T>>
hash_append(
// NOLINTNEXTLINE (runtime/references) Per hash_append convention.
HashAlgorithm& hasher, const T& item) noexcept {
hasher(std::addressof(item), sizeof(item));
}

/// Provides @ref hash_append for bare pointers.
template <class HashAlgorithm, class T>
// NOLINTNEXTLINE (runtime/references) Per hash_append convention.
void hash_append(HashAlgorithm& hasher, const T* item) noexcept {
hash_append(hasher, reinterpret_cast<std::uintptr_t>(item));
};
Expand All @@ -91,6 +93,7 @@ void hash_append(HashAlgorithm& hasher, const T* item) noexcept {
template <class HashAlgorithm, class T>
std::enable_if_t<std::is_enum_v<T>>
hash_append(
// NOLINTNEXTLINE (runtime/references) Per hash_append convention.
HashAlgorithm& hasher, const T& item) noexcept {
hasher(std::addressof(item), sizeof(item));
}
Expand All @@ -99,6 +102,7 @@ hash_append(
template <class HashAlgorithm, class T>
std::enable_if_t<std::is_floating_point_v<T>>
hash_append(
// NOLINTNEXTLINE (runtime/references) Per hash_append convention.
HashAlgorithm& hasher, const T& item) noexcept {
// Hashing a NaN makes no sense, since they cannot compare as equal.
DRAKE_ASSERT(!std::isnan(item));
Expand All @@ -115,6 +119,7 @@ hash_append(
/// (Technically, any string based on `CharT = char`.)
template <class HashAlgorithm, class Traits, class Allocator>
void hash_append(
// NOLINTNEXTLINE (runtime/references) Per hash_append convention.
HashAlgorithm& hasher,
const std::basic_string<char, Traits, Allocator>& item) noexcept {
using drake::hash_append;
Expand All @@ -133,6 +138,7 @@ void hash_append(

/// Provides @ref hash_append for std::pair.
template <class HashAlgorithm, class T1, class T2>
// NOLINTNEXTLINE (runtime/references) Per hash_append convention.
void hash_append(HashAlgorithm& hasher, const std::pair<T1, T2>& item) noexcept;

/// Provides @ref hash_append for std::optional.
Expand All @@ -142,6 +148,7 @@ void hash_append(HashAlgorithm& hasher, const std::pair<T1, T2>& item) noexcept;
/// same hash as that of the value `v` itself. Hash operations implemented
/// with this `hash_append` do *not* provide that invariant.
template <class HashAlgorithm, class T>
// NOLINTNEXTLINE (runtime/references) Per hash_append convention.
void hash_append(HashAlgorithm& hasher, const std::optional<T>& item) noexcept;

/// Provides @ref hash_append for std::map.
Expand All @@ -151,6 +158,7 @@ void hash_append(HashAlgorithm& hasher, const std::optional<T>& item) noexcept;
/// for details.
template <class HashAlgorithm, class T1, class T2, class Compare,
class Allocator>
// NOLINTNEXTLINE (runtime/references) Per hash_append convention.
void hash_append(HashAlgorithm& hasher,
const std::map<T1, T2, Compare, Allocator>& item) noexcept;

Expand All @@ -160,12 +168,14 @@ void hash_append(HashAlgorithm& hasher,
/// [N3980](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n3980.html#unordered)
/// for details.
template <class HashAlgorithm, class Key, class Compare, class Allocator>
// NOLINTNEXTLINE (runtime/references) Per hash_append convention.
void hash_append(HashAlgorithm& hasher,
const std::set<Key, Compare, Allocator>& item) noexcept;

// Now that all of the templated hashers are declared, we can define them.

template <class HashAlgorithm, class T1, class T2>
// NOLINTNEXTLINE (runtime/references) Per hash_append convention.
void hash_append(HashAlgorithm& hasher,
const std::pair<T1, T2>& item) noexcept {
using drake::hash_append;
Expand All @@ -174,6 +184,7 @@ void hash_append(HashAlgorithm& hasher,
}

template <class HashAlgorithm, class T>
// NOLINTNEXTLINE (runtime/references) Per hash_append convention.
void hash_append(HashAlgorithm& hasher, const std::optional<T>& item) noexcept {
if (item) {
hash_append(hasher, *item);
Expand All @@ -184,7 +195,7 @@ void hash_append(HashAlgorithm& hasher, const std::optional<T>& item) noexcept {
/// Provides @ref hash_append for a range, as given by two iterators.
template <class HashAlgorithm, class Iter>
void hash_append_range(
// NOLINTNEXTLINE(runtime/references) Per hash_append convention.
// NOLINTNEXTLINE (runtime/references) Per hash_append convention.
HashAlgorithm& hasher, Iter begin, Iter end) noexcept {
using drake::hash_append;
size_t count{0};
Expand All @@ -198,12 +209,14 @@ void hash_append_range(

template <class HashAlgorithm, class T1, class T2, class Compare,
class Allocator>
// NOLINTNEXTLINE (runtime/references) Per hash_append convention.
void hash_append(HashAlgorithm& hasher,
const std::map<T1, T2, Compare, Allocator>& item) noexcept {
return hash_append_range(hasher, item.begin(), item.end());
};

template <class HashAlgorithm, class Key, class Compare, class Allocator>
// NOLINTNEXTLINE (runtime/references) Per hash_append convention.
void hash_append(HashAlgorithm& hasher,
const std::set<Key, Compare, Allocator>& item) noexcept {
return hash_append_range(hasher, item.begin(), item.end());
Expand Down
1 change: 1 addition & 0 deletions common/identifier.h
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ class Identifier {
confirm it is valid before using it as a key (or other hashing application).
*/
template <typename HashAlgorithm>
// NOLINTNEXTLINE (runtime/references)
friend void hash_append(HashAlgorithm& hasher, const Identifier& i) noexcept {
using drake::hash_append;
hash_append(hasher, i.value_);
Expand Down
4 changes: 3 additions & 1 deletion common/schema/stochastic.cc
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,9 @@ double UniformDiscrete::Mean() const {
"Cannot Mean() empty UniformDiscrete distribution.");
}
double sum = 0;
for (double value : values) { sum += value; }
for (double value : values) {
sum += value;
}
return sum / values.size();
}

Expand Down
1 change: 1 addition & 0 deletions common/sorted_pair.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ struct SortedPair {

/// Implements the @ref hash_append concept.
template <class HashAlgorithm>
// NOLINTNEXTLINE (runtime/references) Per hash_append convention.
friend void hash_append(HashAlgorithm& hasher, const SortedPair& p) noexcept {
using drake::hash_append;
hash_append(hasher, p.first_);
Expand Down
1 change: 1 addition & 0 deletions common/symbolic/chebyshev_basis_element.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ class ChebyshevBasisElement : public PolynomialBasisElement {

/** Implements the @ref hash_append concept. */
template <class HashAlgorithm>
// NOLINTNEXTLINE (runtime/references) Per hash_append convention.
friend void hash_append(HashAlgorithm& hasher,
const ChebyshevBasisElement& item) noexcept {
using drake::hash_append;
Expand Down
1 change: 1 addition & 0 deletions common/symbolic/chebyshev_polynomial.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ class ChebyshevPolynomial {

/** Implements the @ref hash_append concept. */
template <class HashAlgorithm>
// NOLINTNEXTLINE (runtime/references)
friend void hash_append(HashAlgorithm& hasher,
const ChebyshevPolynomial& item) noexcept {
if (item.degree() == 0) {
Expand Down
1 change: 1 addition & 0 deletions common/symbolic/expression/expression.h
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,7 @@ class Expression {

/** Implements the @ref hash_append concept. */
template <class HashAlgorithm>
// NOLINTNEXTLINE (runtime/references) Per hash_append convention.
friend void hash_append(HashAlgorithm& hasher,
const Expression& item) noexcept {
DelegatingHasher delegating_hasher(
Expand Down
1 change: 1 addition & 0 deletions common/symbolic/expression/formula.h
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ class Formula {

/** Implements the @ref hash_append concept. */
template <class HashAlgorithm>
// NOLINTNEXTLINE (runtime/references) Per hash_append convention.
friend void hash_append(HashAlgorithm& hasher, const Formula& item) noexcept {
DelegatingHasher delegating_hasher(
[&hasher](const void* data, const size_t length) {
Expand Down
1 change: 1 addition & 0 deletions common/symbolic/expression/variable.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ class Variable {

/** Implements the @ref hash_append concept. */
template <class HashAlgorithm>
// NOLINTNEXTLINE (runtime/references) Per hash_append convention.
friend void hash_append(HashAlgorithm& hasher,
const Variable& item) noexcept {
using drake::hash_append;
Expand Down
1 change: 1 addition & 0 deletions common/symbolic/expression/variables.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ class Variables {

/** Implements the @ref hash_append concept. */
template <class HashAlgorithm>
// NOLINTNEXTLINE (runtime/references) Per hash_append convention.
friend void hash_append(HashAlgorithm& hasher,
const Variables& item) noexcept {
using drake::hash_append;
Expand Down
1 change: 1 addition & 0 deletions common/symbolic/generic_polynomial.h
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ class GenericPolynomial {
/** Implements the @ref hash_append concept. */
template <class HashAlgorithm>
friend void hash_append(
// NOLINTNEXTLINE (runtime/references) Per hash_append convention.
HashAlgorithm& hasher,
const GenericPolynomial<BasisElement>& item) noexcept {
using drake::hash_append;
Expand Down
1 change: 1 addition & 0 deletions common/symbolic/monomial.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ class Monomial {

/** Implements the @ref hash_append concept. */
template <class HashAlgorithm>
// NOLINTNEXTLINE (runtime/references) Per hash_append convention.
friend void hash_append(HashAlgorithm& hasher,
const Monomial& item) noexcept {
using drake::hash_append;
Expand Down
1 change: 1 addition & 0 deletions common/symbolic/monomial_basis_element.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ class MonomialBasisElement : public PolynomialBasisElement {

/** Implements the @ref hash_append concept. */
template <class HashAlgorithm>
// NOLINTNEXTLINE (runtime/references) Per hash_append convention.
friend void hash_append(HashAlgorithm& hasher,
const MonomialBasisElement& item) noexcept {
using drake::hash_append;
Expand Down
1 change: 1 addition & 0 deletions common/symbolic/polynomial.h
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,7 @@ class Polynomial {

/** Implements the @ref hash_append concept. */
template <class HashAlgorithm>
// NOLINTNEXTLINE (runtime/references) Per hash_append convention.
friend void hash_append(HashAlgorithm& hasher,
const Polynomial& item) noexcept {
using drake::hash_append;
Expand Down
16 changes: 12 additions & 4 deletions common/test/identifier_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,9 @@ TEST_F(IdentifierTests, Convertible) {

// Attempting to acquire the value is an error.
TEST_F(IdentifierTests, InvalidGetValueCall) {
if (kDrakeAssertIsDisarmed) { return; }
if (kDrakeAssertIsDisarmed) {
return;
}
AId invalid;
DRAKE_EXPECT_THROWS_MESSAGE(
invalid.get_value(),
Expand All @@ -256,14 +258,18 @@ TEST_F(IdentifierTests, InvalidGetValueCall) {

// Comparison of invalid ids is an error.
TEST_F(IdentifierTests, InvalidEqualityCompare) {
if (kDrakeAssertIsDisarmed) { return; }
if (kDrakeAssertIsDisarmed) {
return;
}
AId invalid;
DRAKE_EXPECT_THROWS_MESSAGE(invalid == a1_, ".*is_valid.*failed.*");
}

// Comparison of invalid ids is an error.
TEST_F(IdentifierTests, InvalidInequalityCompare) {
if (kDrakeAssertIsDisarmed) { return; }
if (kDrakeAssertIsDisarmed) {
return;
}
AId invalid;
DRAKE_EXPECT_THROWS_MESSAGE(invalid != a1_, ".*is_valid.*failed.*");
}
Expand All @@ -290,7 +296,9 @@ TEST_F(IdentifierTests, InvalidHash) {

// Streaming an invalid id is an error.
TEST_F(IdentifierTests, InvalidStream) {
if (kDrakeAssertIsDisarmed) { return; }
if (kDrakeAssertIsDisarmed) {
return;
}
AId invalid;
std::stringstream ss;
DRAKE_EXPECT_THROWS_MESSAGE(ss << invalid, ".*is_valid.*failed.*");
Expand Down
12 changes: 9 additions & 3 deletions common/test_utilities/limit_malloc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,9 @@ class ActiveMonitor {
};

void Monitor::ObserveAllocation() {
if (!IsSupportedConfiguration()) { return; }
if (!IsSupportedConfiguration()) {
return;
}

bool failure = false;

Expand All @@ -191,7 +193,9 @@ void Monitor::ObserveAllocation() {

// TODO(jwnimmer-tri) Add more limits (requested bytes?) here.

if (!failure) { return; }
if (!failure) {
return;
}

// Non-fatal breakpoint action; use with helper script:
// tools/dynamic_analysis/dump_limit_malloc_stacks
Expand Down Expand Up @@ -284,7 +288,9 @@ void* realloc(void* ptr, size_t size) {
}

static void EvaluateMinNumAllocations(int observed, int min_num_allocations) {
if (!drake::test::IsSupportedConfiguration()) { return; }
if (!drake::test::IsSupportedConfiguration()) {
return;
}

if ((min_num_allocations >= 0) && (observed < min_num_allocations)) {
std::cerr << "abort due to scope end with "
Expand Down
12 changes: 9 additions & 3 deletions common/test_utilities/test/limit_malloc_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,29 @@ void CallMalloc() {
void* dummy = malloc(16);
g_dummy = dummy;
free(dummy);
if (g_dummy == nullptr) { throw std::runtime_error("null dummy"); }
if (g_dummy == nullptr) {
throw std::runtime_error("null dummy");
}
}

// Calls calloc (and then immediately frees).
void CallCalloc() {
void* dummy = calloc(1, 16);
g_dummy = dummy;
free(dummy);
if (g_dummy == nullptr) { throw std::runtime_error("null dummy"); }
if (g_dummy == nullptr) {
throw std::runtime_error("null dummy");
}
}

// Calls realloc (and then immediately frees).
void CallRealloc() {
void* dummy = realloc(nullptr, 16);
g_dummy = dummy;
free(dummy);
if (g_dummy == nullptr) { throw std::runtime_error("null dummy"); }
if (g_dummy == nullptr) {
throw std::runtime_error("null dummy");
}
}

// A value-parameterized test fixture.
Expand Down
1 change: 1 addition & 0 deletions common/type_safe_index.h
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,7 @@ class TypeSafeIndex {
/// the user to confirm it is valid before using it as a key (or other hashing
/// application).
template <typename HashAlgorithm>
// NOLINTNEXTLINE (runtime/references)
friend void hash_append(HashAlgorithm& hasher,
const TypeSafeIndex& i) noexcept {
using drake::hash_append;
Expand Down
1 change: 1 addition & 0 deletions geometry/render/render_label.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ class RenderLabel {

/** Implements the @ref hash_append concept. */
template <class HashAlgorithm>
// NOLINTNEXTLINE (runtime/references) Per hash_append convention.
friend void hash_append(HashAlgorithm& hasher,
const RenderLabel& item) noexcept {
using drake::hash_append;
Expand Down
1 change: 1 addition & 0 deletions geometry/render_gl/internal_buffer_dim.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class BufferDim {

/* Implements the @ref hash_append concept. */
template <class HashAlgorithm>
// NOLINTNEXTLINE (runtime/references)
friend void hash_append(HashAlgorithm& hasher,
const BufferDim& dim) noexcept {
using drake::hash_append;
Expand Down
4 changes: 3 additions & 1 deletion geometry/scene_graph_config.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ void ThrowUnlessAbsentOr(
std::string_view name,
std::optional<double> property,
Condition condition) {
if (!property.has_value()) { return; }
if (!property.has_value()) {
return;
}
double value = *property;
std::string_view condition_name;
bool should_throw{false};
Expand Down
1 change: 1 addition & 0 deletions math/rigid_transform.h
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,7 @@ class RigidTransform {
/// Implements the @ref hash_append concept.
/// @pre T implements the hash_append concept.
template <class HashAlgorithm>
// NOLINTNEXTLINE (runtime/references) Per hash_append convention.
friend void hash_append(HashAlgorithm& hasher,
const RigidTransform& X) noexcept {
using drake::hash_append;
Expand Down
1 change: 1 addition & 0 deletions math/roll_pitch_yaw.h
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,7 @@ class RollPitchYaw {
/// Implements the @ref hash_append concept.
/// @pre T implements the hash_append concept.
template <class HashAlgorithm>
// NOLINTNEXTLINE (runtime/references) Per hash_append convention.
friend void hash_append(HashAlgorithm& hasher,
const RollPitchYaw& rpy) noexcept {
const T* begin = rpy.roll_pitch_yaw_.data();
Expand Down
1 change: 1 addition & 0 deletions math/rotation_matrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -723,6 +723,7 @@ class RotationMatrix {
/// Implements the @ref hash_append concept.
/// @pre T implements the hash_append concept.
template <class HashAlgorithm>
// NOLINTNEXTLINE (runtime/references) Per hash_append convention.
friend void hash_append(HashAlgorithm& hasher,
const RotationMatrix& R) noexcept {
const T* begin = R.R_AB_.data();
Expand Down
4 changes: 2 additions & 2 deletions multibody/mpm/particle_sorter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,12 @@ void ParticleSorter::ComputeBitInformation(const SpGridFlags& flags,
// TODO(xuchenhan-tri): This should be checked when we register particles to
// the MPM model.
/* Confirm that index_bits_ is enough to store num_particles. */
const uint64_t capacity = (uint64_t(1) << index_bits_);
const uint64_t capacity = (static_cast<uint64_t>(1) << index_bits_);
DRAKE_DEMAND(capacity > static_cast<uint64_t>(num_particles));
}

void ParticleSorter::UnpackResults() {
const uint64_t index_mask = ((uint64_t(1) << index_bits_) - 1);
const uint64_t index_mask = ((static_cast<uint64_t>(1) << index_bits_) - 1);

for (int p = 0; p < static_cast<int>(particle_sorters_.size()); ++p) {
/* Lower bits store the particle's original index. */
Expand Down
Loading