Skip to content

Add clang-tidy checks for Firestore #1078

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

Merged
merged 5 commits into from
Apr 12, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
34 changes: 34 additions & 0 deletions Firestore/core/.clang-tidy
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
---
# cert-*
# -cert-dcl50-cpp
# We use variadic functions
# -cert-err58-cpp
# GoogleTest creates instances in static scope in a way that trips this
# warning for every test.
# readability-*
# -readability-else-after-return
# -readability-implicit-bool-conversion
# These checks generate a bunch of noise that we're just not religious
# about.
# modernize-*
# -modernize-use-equals-default
# VS 2015 and Xcode <= 8.2 don't fully support this so we don't use
# `= default`.
# -modernize-use-equals-delete
# GoogleTest generates test classes that use the old idiom of making
# default constructors and operator= private.
Checks: 'bugprone-*,cert-*,-cert-dcl50-cpp,-cert-err58-cpp,google-*,objc-*,readability-*,-readability-else-after-return,-readability-implicit-bool-conversion,misc-*,modernize-*,-modernize-use-equals-default,-modernize-use-equals-delete,clang-diagnostic-*,clang-analyzer-*'
WarningsAsErrors: ''
HeaderFilterRegex: ''
CheckOptions:
- key: readability-braces-around-statements.ShortStatementLines
value: '1'
- key: google-readability-braces-around-statements.ShortStatementLines
value: '1'
- key: google-readability-function-size.StatementThreshold
value: '800'
- key: google-readability-namespace-comments.ShortNamespaceLines
value: '10'
- key: google-readability-namespace-comments.SpacesBeforeComments
value: '2'
...
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

// TODO(rsgowman): This file isn't intended to be used just yet. It's just an
// outline of what the API might eventually look like. Most of this was
// shamelessly stolen and modified from rtdb's header file, melded with the
// shamelessly stolen and modified from RTDB's header file, melded with the
// (java) firestore api.

#ifndef FIRESTORE_CORE_INCLUDE_FIREBASE_FIRESTORE_DOCUMENT_REFERENCE_H_
Expand All @@ -36,7 +36,7 @@
// here so we don't forget to mention this during the API review, and should be
// removed once this note has migrated to the API review doc.

// TODO(rsgowman): replace these forward decl's with appropriate includes (once
// TODO(rsgowman): replace these forward decls with appropriate includes (once
// they exist)
namespace firebase {
class App;
Expand All @@ -47,7 +47,7 @@ class Future;
namespace firebase {
namespace firestore {

// TODO(rsgowman): replace these forward decl's with appropriate includes (once
// TODO(rsgowman): replace these forward decls with appropriate includes (once
// they exist)
class FieldValue;
class DocumentSnapshot;
Expand Down Expand Up @@ -361,7 +361,7 @@ namespace std {
// C++ style guide. But we think this is probably ok in this case since:
// a) It's the standard way of doing this outside of Google (as the style guide
// itself points out), and
// b) This has a straightfoward hash function anyway (just hash the path) so I
// b) This has a straightforward hash function anyway (just hash the path) so I
// don't think the concerns in the style guide are going to bite us.
//
// Raise this concern during the API review.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ namespace firestore {
namespace auth {

// `TokenErrorListener` is a listener that gets a token or an error.
typedef std::function<void(util::StatusOr<Token>)> TokenListener;
using TokenListener = std::function<void(util::StatusOr<Token>)>;

// Listener notified with a User change.
typedef std::function<void(User user)> UserChangeListener;
using UserChangeListener = std::function<void(User user)>;

/**
* Provides methods for getting the uid and token for the current user and
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class FirebaseCredentialsProvider : public CredentialsProvider {
*/
struct Contents {
Contents(FIRApp* app, User&& user)
: app(app), current_user(std::move(user)), mutex() {
: app(app), current_user(std::move(user)) {
}

const FIRApp* app;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "Firestore/core/src/firebase/firestore/util/string_apple.h"

// NB: This is also defined in Firestore/Source/Public/FIRFirestoreErrors.h
// NOLINTNEXTLINE: public constant
NSString* const FIRFirestoreErrorDomain = @"FIRFirestoreErrorDomain";

namespace firebase {
Expand All @@ -47,7 +48,7 @@
std::unique_lock<std::mutex> lock(contents->mutex);
NSDictionary<NSString*, id>* user_info = notification.userInfo;

// ensure we're only notifiying for the current app.
// ensure we're only notifying for the current app.
FIRApp* notified_app =
user_info[FIRAuthStateDidChangeInternalNotificationAppKey];
if (![contents->app isEqual:notified_app]) {
Expand Down
2 changes: 1 addition & 1 deletion Firestore/core/src/firebase/firestore/auth/token.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ namespace auth {
// TODO(zxu123): Make this support token-type for desktop workflow.
class Token {
public:
Token(const absl::string_view token, const User& user);
Token(absl::string_view token, const User& user);

/** The actual raw token. */
const std::string& token() const {
Expand Down
2 changes: 1 addition & 1 deletion Firestore/core/src/firebase/firestore/auth/user.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace firebase {
namespace firestore {
namespace auth {

User::User() : uid_(), is_authenticated_(false) {
User::User() : is_authenticated_(false) {
}

User::User(const absl::string_view uid) : uid_(uid), is_authenticated_(true) {
Expand Down
2 changes: 1 addition & 1 deletion Firestore/core/src/firebase/firestore/auth/user.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class User {
User();

/** Construct an authenticated user with the given UID. */
explicit User(const absl::string_view uid);
explicit User(absl::string_view uid);

const std::string& uid() const {
return uid_;
Expand Down
6 changes: 3 additions & 3 deletions Firestore/core/src/firebase/firestore/core/database_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ class DatabaseInfo {
* @param database_id The project/database to use.
* @param persistence_key A unique identifier for this Firestore's local
* storage. Usually derived from -[FIRApp appName].
* @param host The hostname of the datastore backend.
* @param host The hostname of the Firestore backend.
* @param ssl_enabled Whether to use SSL when connecting.
*/
DatabaseInfo(const firebase::firestore::model::DatabaseId& database_id,
const absl::string_view persistence_key,
const absl::string_view host,
absl::string_view persistence_key,
absl::string_view host,
bool ssl_enabled);

const firebase::firestore::model::DatabaseId& database_id() const {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ class FixedArray {
*/
template <typename SourceIterator>
void append(SourceIterator src_begin, SourceIterator src_end) {
size_type appending = static_cast<size_type>(src_end - src_begin);
size_type new_size = size_ + appending;
auto appending = static_cast<size_type>(src_end - src_begin);
auto new_size = size_ + appending;
FIREBASE_ASSERT(new_size <= fixed_size);

std::copy(src_begin, src_end, end());
Expand Down Expand Up @@ -229,8 +229,6 @@ class ArraySortedMap : public SortedMapBase {
}
}

// TODO(wilhuff): indexof

/** Returns true if the map contains no elements. */
bool empty() const {
return size() == 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ namespace immutable {
*/
template <typename K, typename V, typename C = std::less<K>>
struct KeyComparator {
typedef std::pair<K, V> pair_type;
using pair_type = std::pair<K, V>;

explicit KeyComparator(const C& comparator = C())
: key_comparator_(comparator) {
Expand Down
2 changes: 1 addition & 1 deletion Firestore/core/src/firebase/firestore/local/leveldb_key.cc
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ DocumentKey Reader::ReadDocumentKey() {
}

ResourcePath path{std::move(path_segments)};
if (ok_ && path.size() > 0 && DocumentKey::IsDocumentKey(path)) {
if (ok_ && !path.empty() && DocumentKey::IsDocumentKey(path)) {
return DocumentKey{std::move(path)};
}

Expand Down
3 changes: 1 addition & 2 deletions Firestore/core/src/firebase/firestore/model/database_id.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ class DatabaseId {
* @param project_id The project for the database.
* @param database_id The database in the project to use.
*/
DatabaseId(const absl::string_view project_id,
const absl::string_view database_id);
DatabaseId(absl::string_view project_id, absl::string_view database_id);

const std::string& project_id() const {
return project_id_;
Expand Down
2 changes: 1 addition & 1 deletion Firestore/core/src/firebase/firestore/model/document.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ bool Document::Equals(const MaybeDocument& other) const {
if (other.type() != Type::Document) {
return false;
}
const Document& other_doc = static_cast<const Document&>(other);
auto& other_doc = static_cast<const Document&>(other);
return MaybeDocument::Equals(other) &&
has_local_mutations_ == other_doc.has_local_mutations_ &&
data_ == other_doc.data_;
Expand Down
4 changes: 1 addition & 3 deletions Firestore/core/src/firebase/firestore/model/field_mask.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,7 @@ class FieldMask {

FieldMask(std::initializer_list<FieldPath> list) : fields_{list} {
}
explicit FieldMask(const std::vector<FieldPath>& fields) : fields_{fields} {
}
explicit FieldMask(std::vector<FieldPath>&& fields)
explicit FieldMask(std::vector<FieldPath> fields)
: fields_{std::move(fields)} {
}

Expand Down
5 changes: 3 additions & 2 deletions Firestore/core/src/firebase/firestore/model/field_value.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ namespace {
/**
* This deviates from the other platforms that define TypeOrder. Since
* we already define Type for union types, we use it together with this
* function to achive the equivalent order of types i.e.
* function to achieve the equivalent order of types i.e.
* i) if two types are comparable, then they are of equal order;
* ii) otherwise, their order is the same as the order of their Type.
*/
Expand Down Expand Up @@ -148,7 +148,8 @@ FieldValue& FieldValue::operator=(FieldValue&& value) {
return *this;
default:
// We just copy over POD union types.
return *this = value;
*this = value;
return *this;
}
}

Expand Down
2 changes: 1 addition & 1 deletion Firestore/core/src/firebase/firestore/model/field_value.h
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ class FieldValue {
/**
* Switch to the specified type, if different from the current type.
*/
void SwitchTo(const Type type);
void SwitchTo(Type type);

Type tag_ = Type::Null;
union {
Expand Down
2 changes: 1 addition & 1 deletion Firestore/core/src/firebase/firestore/remote/serializer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ void Writer::WriteVarint(uint64_t value) {
* Note that (despite the return type) this works for bool, enum, int32, int64,
* uint32 and uint64 proto field types.
*
* Note: This is not expected to be called direclty, but rather only via the
* Note: This is not expected to be called directly, but rather only via the
* other Decode* methods (i.e. DecodeBool, DecodeLong, etc)
*
* @return The decoded varint as a uint64_t.
Expand Down
10 changes: 4 additions & 6 deletions Firestore/core/src/firebase/firestore/remote/serializer.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class Serializer {
// TODO(rsgowman): If we never support any output except to a vector, it may
// make sense to have Serializer own the vector and provide an accessor rather
// than asking the user to create it first.
static util::Status EncodeFieldValue(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you elaborate why remove static here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The tests instantiate the serializer and then access these static methods through the instance, which causes a warning under clang-tidy.

In the fullness of time the serializer will need to be stateful (for example, to encode a DocumentKey you need to know the current DatabaseId) so fixing those warnings by removing the static seemed like the better way.

util::Status EncodeFieldValue(
const firebase::firestore::model::FieldValue& field_value,
std::vector<uint8_t>* out_bytes);

Expand All @@ -84,8 +84,7 @@ class Serializer {
* @return The model equivalent of the bytes.
*/
// TODO(rsgowman): error handling.
static firebase::firestore::model::FieldValue DecodeFieldValue(
const uint8_t* bytes, size_t length);
model::FieldValue DecodeFieldValue(const uint8_t* bytes, size_t length);

/**
* @brief Converts from bytes to the model FieldValue format.
Expand All @@ -95,14 +94,13 @@ class Serializer {
* @return The model equivalent of the bytes.
*/
// TODO(rsgowman): error handling.
static firebase::firestore::model::FieldValue DecodeFieldValue(
const std::vector<uint8_t>& bytes) {
model::FieldValue DecodeFieldValue(const std::vector<uint8_t>& bytes) {
return DecodeFieldValue(bytes.data(), bytes.size());
}

private:
// TODO(rsgowman): We don't need the database_id_ yet (but will eventually).
// const firebase::firestore::model::DatabaseId& database_id_;
// model::DatabaseId* database_id_;
};

} // namespace remote
Expand Down
2 changes: 1 addition & 1 deletion Firestore/core/src/firebase/firestore/timestamp.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ Timestamp Timestamp::Now() {
}

Timestamp Timestamp::FromTimeT(const time_t seconds_since_unix_epoch) {
return Timestamp(seconds_since_unix_epoch, 0);
return {seconds_since_unix_epoch, 0};
}

#if !defined(_STLPORT_VERSION)
Expand Down
12 changes: 6 additions & 6 deletions Firestore/core/src/firebase/firestore/util/bits.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,23 +139,23 @@ inline int Bits::Log2FloorNonZero_Portable(uint32_t n) {

// Log2Floor64() is defined in terms of Log2Floor32(), Log2FloorNonZero32()
inline int Bits::Log2Floor64_Portable(uint64_t n) {
const uint32_t topbits = static_cast<uint32_t>(n >> 32);
if (topbits == 0) {
const auto top_bits = static_cast<uint32_t>(n >> 32);
if (top_bits == 0) {
// Top bits are zero, so scan in bottom bits
return Log2Floor(static_cast<uint32_t>(n));
} else {
return 32 + Log2FloorNonZero(topbits);
return 32 + Log2FloorNonZero(top_bits);
}
}

// Log2FloorNonZero64() is defined in terms of Log2FloorNonZero32()
inline int Bits::Log2FloorNonZero64_Portable(uint64_t n) {
const uint32_t topbits = static_cast<uint32_t>(n >> 32);
if (topbits == 0) {
const auto top_bits = static_cast<uint32_t>(n >> 32);
if (top_bits == 0) {
// Top bits are zero, so scan in bottom bits
return Log2FloorNonZero(static_cast<uint32_t>(n));
} else {
return 32 + Log2FloorNonZero(topbits);
return 32 + Log2FloorNonZero(top_bits);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class ComparatorHolder {
template <typename C>
class ComparatorHolder<C, true> : private C {
protected:
explicit ComparatorHolder(const C&) noexcept {
explicit ComparatorHolder(const C& /* comparator */) noexcept {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe put the parameter name outside of comment like
explicit ComparatorHolder(const C& comparator) noexcept {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That causes a different warning about an unused parameter. This way both the compiler and the linter are happy.

I could also do

explicit ComparatorHolder(const C& comparator) noexcept {
  (void)comparator;
}

But considered this to be inferior. I have no strong preference though.

}

const C& comparator() const noexcept {
Expand Down
2 changes: 1 addition & 1 deletion Firestore/core/src/firebase/firestore/util/comparison.cc
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ ComparisonResult CompareMixedNumber(double double_value, int64_t int64_value) {

// At this point the long representations are equal but this could be due to
// rounding.
double int64_as_double = static_cast<double>(int64_value);
auto int64_as_double = static_cast<double>(int64_value);
return Compare<double>(double_value, int64_as_double);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ namespace firestore {
namespace immutable {
namespace impl {

typedef ArraySortedMap<int, int> IntMap;
using IntMap = ArraySortedMap<int, int>;
constexpr IntMap::size_type kFixedSize = IntMap::kFixedSize;

// TODO(wilhuff): ReverseTraversal
Expand Down Expand Up @@ -153,7 +153,7 @@ TEST(ArraySortedMap, EmptyRemoval) {

TEST(ArraySortedMap, InsertionAndRemovalOfMaxItems) {
auto expected_size = kFixedSize;
int n = static_cast<int>(expected_size);
auto n = static_cast<int>(expected_size);
std::vector<int> to_insert = Shuffled(Sequence(n));
std::vector<int> to_remove = Shuffled(to_insert);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class SortedMapTest : public ::testing::Test {
}
};

// NOLINTNEXTLINE: must be a typedef for the gtest macros
typedef ::testing::Types<SortedMap<int, int>,
impl::ArraySortedMap<int, int>,
impl::TreeSortedMap<int, int>>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ namespace firestore {
namespace immutable {
namespace impl {

typedef TreeSortedMap<int, int> IntMap;
using IntMap = TreeSortedMap<int, int>;

TEST(TreeSortedMap, EmptySize) {
IntMap map;
Expand Down
Loading