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

Safety: Use proper ref specifiers on methods that returns reference to members #6688

Closed
wants to merge 2 commits into from
Closed
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
2 changes: 1 addition & 1 deletion lib/analyzer.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ struct Analyzer {
return get(Match);
}

Action& operator|=(Action a) {
Action& operator|=(Action a) & {
set(a.mFlag);
return *this;
}
Expand Down
5 changes: 4 additions & 1 deletion lib/check.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,10 @@ class CPPCHECKLIB Check {
virtual void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) const = 0;

/** class name, used to generate documentation */
const std::string& name() const {
const std::string& name() const & {
return mName;
}
std::string name() && {
return mName;
}

Expand Down
5 changes: 4 additions & 1 deletion lib/checkunusedvar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,10 @@ class Variables {
void clear() {
mVarUsage.clear();
}
const std::map<nonneg int, VariableUsage> &varUsage() const {
const std::map<nonneg int, VariableUsage> &varUsage() const & {
return mVarUsage;
}
std::map<nonneg int, VariableUsage> varUsage() && {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I do no think we should add/use && unless absolutely necessary.

Especially in this case it makes no sense at all since as this class will only exist once as an global instance.

return mVarUsage;
}
void addVar(const Variable *var, VariableType type, bool write_);
Expand Down
2 changes: 1 addition & 1 deletion lib/cppcheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1511,7 +1511,7 @@ void CppCheck::executeAddonsWholeProgram(const std::list<FileWithDetails> &files
}
}

Settings &CppCheck::settings()
Settings &CppCheck::settings()&
{
return mSettings;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/cppcheck.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ class CPPCHECKLIB CppCheck : ErrorLogger {
* @brief Get reference to current settings.
* @return a reference to current settings
*/
Settings &settings();
Settings &settings()&;

/**
* @brief Returns current version number as a string.
Expand Down
24 changes: 20 additions & 4 deletions lib/errorlogger.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,11 @@ class CPPCHECKLIB ErrorMessage {
int line; // negative value means "no line"
unsigned int column;

const std::string& getinfo() const {
const std::string& getinfo() const & {
return mInfo;
}

std::string getinfo() && {
return mInfo;
}

Expand Down Expand Up @@ -181,18 +185,30 @@ class CPPCHECKLIB ErrorMessage {
void setmsg(const std::string &msg);

/** Short message (single line short message) */
const std::string &shortMessage() const {
const std::string &shortMessage() const & {
return mShortMessage;
}

std::string shortMessage() && {
return mShortMessage;
}

/** Verbose message (may be the same as the short message) */
// cppcheck-suppress unusedFunction - used by GUI only
const std::string &verboseMessage() const {
const std::string &verboseMessage() const & {
return mVerboseMessage;
}

std::string verboseMessage() && {
return mVerboseMessage;
}

/** Symbol names */
const std::string &symbolNames() const {
const std::string &symbolNames() const & {
return mSymbolNames;
}

std::string symbolNames() && {
return mSymbolNames;
}

Expand Down
13 changes: 11 additions & 2 deletions lib/filesettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,21 @@ class FileWithDetails
throw std::runtime_error("empty path specified");
}

const std::string& path() const
const std::string& path() const &
{
return mPath;
}

const std::string& spath() const
std::string path() &&
{
return mPath;
}

const std::string& spath() const &
{
return mPathSimplified;
}
std::string spath() &&
{
return mPathSimplified;
}
Expand Down
10 changes: 8 additions & 2 deletions lib/library.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,16 @@ struct Library::LibraryData
void addBlock(const char* blockName) {
mBlocks.insert(blockName);
}
const std::string& start() const {
const std::string& start() const & {
return mStart;
}
const std::string& end() const {
std::string start() && {
return mStart;
}
const std::string& end() const & {
return mEnd;
}
std::string end() && {
return mEnd;
}
int offset() const {
Expand Down
7 changes: 6 additions & 1 deletion lib/suppressions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,12 @@ std::list<SuppressionList::Suppression> SuppressionList::getUnmatchedGlobalSuppr
return result;
}

const std::list<SuppressionList::Suppression> &SuppressionList::getSuppressions() const
const std::list<SuppressionList::Suppression> &SuppressionList::getSuppressions() const &
{
return mSuppressions;
}

std::list<SuppressionList::Suppression> SuppressionList::getSuppressions() &&
{
return mSuppressions;
}
Expand Down
8 changes: 6 additions & 2 deletions lib/suppressions.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ class CPPCHECKLIB SuppressionList {
std::size_t hash;
std::string errorId;
void setFileName(std::string s);
const std::string &getFileName() const {
const std::string &getFileName() const& {
return mFileName;
}
std::string getFileName() && {
return mFileName;
}
int lineNumber;
Expand Down Expand Up @@ -241,7 +244,8 @@ class CPPCHECKLIB SuppressionList {
* @brief Returns list of all suppressions.
* @return list of suppressions
*/
const std::list<Suppression> &getSuppressions() const;
const std::list<Suppression> &getSuppressions() const&;
std::list<Suppression> getSuppressions() &&;

/**
* @brief Marks Inline Suppressions as checked if source line is in the token stream
Expand Down
8 changes: 6 additions & 2 deletions lib/symboldatabase.h
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,10 @@ class CPPCHECKLIB Variable {
* Get array dimensions.
* @return array dimensions vector
*/
const std::vector<Dimension> &dimensions() const {
const std::vector<Dimension> &dimensions() const& {
return mDimensions;
}
std::vector<Dimension> dimensions() && {
return mDimensions;
}

Expand Down Expand Up @@ -1373,9 +1376,10 @@ class CPPCHECKLIB SymbolDatabase {
return mVariableList.at(varId);
}

const std::vector<const Variable *> & variableList() const {
const std::vector<const Variable *> & variableList() const& {
return mVariableList;
}
std::vector<const Variable *> variableList() &&; // Unimplemented by intention; Not safe to use this on temporary object
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we use = delete here?


/**
* @brief output a debug message
Expand Down
20 changes: 16 additions & 4 deletions lib/templatesimplifier.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ class CPPCHECKLIB TemplateSimplifier {
public:
explicit TemplateSimplifier(Tokenizer &tokenizer);

const std::string& dump() const {
const std::string& dump() const & {
return mDump;
}
std::string dump() && {
return mDump;
}

Expand Down Expand Up @@ -169,13 +172,22 @@ class CPPCHECKLIB TemplateSimplifier {
void token(Token * token) {
mToken = token;
}
const std::string & scope() const {
const std::string & scope() const & {
return mScope;
}
std::string scope() && {
return mScope;
}
const std::string & name() const {
const std::string & name() const & {
return mName;
}
const std::string & fullName() const {
std::string name() && {
return mName;
}
const std::string & fullName() const & {
return mFullName;
}
std::string fullName() && {
return mFullName;
}
const Token * nameToken() const {
Expand Down
5 changes: 4 additions & 1 deletion lib/token.h
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,10 @@ class CPPCHECKLIB Token {
*/
void concatStr(std::string const& b);

const std::string &str() const {
const std::string &str() const & {
return mStr;
}
std::string str() && {
return mStr;
}

Expand Down
5 changes: 4 additions & 1 deletion lib/tokenlist.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,10 @@ class CPPCHECKLIB TokenList {
* The first filename is the filename for the sourcefile
* @return vector with filenames
*/
const std::vector<std::string>& getFiles() const {
const std::vector<std::string>& getFiles() const & {
return mFiles;
}
std::vector<std::string> getFiles() && {
return mFiles;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/tokenrange.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class TokenRangeBase {
T* mt;
TokenIterator() : mt(nullptr) {}
explicit TokenIterator(T* t) : mt(t) {}
TokenIterator& operator++() {
TokenIterator& operator++() & {
mt = mt->next();
return *this;
}
Expand Down
12 changes: 9 additions & 3 deletions lib/valueflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -759,7 +759,7 @@ class SelectValueFromVarIdMapRange {
return &mIt->second;
}

Iterator &operator++() {
Iterator &operator++() & {
// cppcheck-suppress postfixOperator - forward iterator needs to perform post-increment
mIt++;
return *this;
Expand Down Expand Up @@ -1529,11 +1529,17 @@ struct SingleValueFlowAnalyzer : ValueFlowAnalyzer {

SingleValueFlowAnalyzer(ValueFlow::Value v, const Settings& s) : ValueFlowAnalyzer(s), value(std::move(v)) {}

const std::unordered_map<nonneg int, const Variable*>& getVars() const {
const std::unordered_map<nonneg int, const Variable*>& getVars() const & {
return varids;
}
std::unordered_map<nonneg int, const Variable*> getVars() && {
return varids;
}

const std::unordered_map<nonneg int, const Variable*>& getAliasedVars() const {
const std::unordered_map<nonneg int, const Variable*>& getAliasedVars() const & {
return aliases;
}
std::unordered_map<nonneg int, const Variable*> getAliasedVars() && {
return aliases;
}

Expand Down
Loading