Skip to content

Commit

Permalink
ICU-22920 Avoid "return by const value" antipattern
Browse files Browse the repository at this point in the history
Returning a const-qualified prvalue doesn't do anything useful, but it does
turn an assignment such as `v = rb.getLocale();` from a move-assignment
into a copy-assignment (because it's forbidden to move-from a const value,
even if it's a const prvalue). Each affected site was diagnosed mechanically
by my fork of Clang. E.g.:

    warning: 'const' type qualifier on return type is a bad idea [-Wqual-class-return-type]
      391 | const Locale ResourceBundle::getLocale(ULocDataLocaleType type, UErrorCode &status) const
          | ^~~~~
  • Loading branch information
Quuxplusone authored and markusicu committed Jan 30, 2025
1 parent d61ec78 commit fb64693
Show file tree
Hide file tree
Showing 7 changed files with 8 additions and 8 deletions.
2 changes: 1 addition & 1 deletion icu4c/source/common/resbund.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ const Locale &ResourceBundle::getLocale() const {
return ncThis->fLocale != nullptr ? *ncThis->fLocale : Locale::getDefault();
}

const Locale ResourceBundle::getLocale(ULocDataLocaleType type, UErrorCode &status) const
Locale ResourceBundle::getLocale(ULocDataLocaleType type, UErrorCode &status) const
{
return ures_getLocaleByType(fResource, type, &status);
}
Expand Down
2 changes: 1 addition & 1 deletion icu4c/source/common/unicode/resbund.h
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ class U_COMMON_API ResourceBundle : public UObject {
* @return a Locale object
* @stable ICU 2.8
*/
const Locale
Locale
getLocale(ULocDataLocaleType type, UErrorCode &status) const;
#ifndef U_HIDE_INTERNAL_API
/**
Expand Down
2 changes: 1 addition & 1 deletion icu4c/source/i18n/timezone.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1051,7 +1051,7 @@ TimeZone::countEquivalentIDs(const UnicodeString& id) {

// ---------------------------------------

const UnicodeString U_EXPORT2
UnicodeString U_EXPORT2
TimeZone::getEquivalentID(const UnicodeString& id, int32_t index) {
U_DEBUG_TZ_MSG(("gEI(%d)\n", index));
UnicodeString result;
Expand Down
2 changes: 1 addition & 1 deletion icu4c/source/i18n/unicode/messageformat2_data_model.h
Original file line number Diff line number Diff line change
Expand Up @@ -2330,7 +2330,7 @@ namespace message2 {
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
const std::vector<VariableName> getSelectors() const {
std::vector<VariableName> getSelectors() const {
if (std::holds_alternative<Pattern>(body)) {
return {};
}
Expand Down
2 changes: 1 addition & 1 deletion icu4c/source/i18n/unicode/timezone.h
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ class U_I18N_API TimeZone : public UObject {
* @see #countEquivalentIDs
* @stable ICU 2.0
*/
static const UnicodeString U_EXPORT2 getEquivalentID(const UnicodeString& id,
static UnicodeString U_EXPORT2 getEquivalentID(const UnicodeString& id,
int32_t index);

/**
Expand Down
2 changes: 1 addition & 1 deletion icu4c/source/tools/ctestfw/datamap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ const ResourceBundle *RBDataMap::getItem(const char* key, UErrorCode &status) co
}
}

const UnicodeString RBDataMap::getString(const char* key, UErrorCode &status) const
UnicodeString RBDataMap::getString(const char* key, UErrorCode &status) const
{
const ResourceBundle *r = getItem(key, status);
if(U_SUCCESS(status)) {
Expand Down
4 changes: 2 additions & 2 deletions icu4c/source/tools/ctestfw/unicode/datamap.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class T_CTEST_EXPORT_API DataMap {
* @param key name of the data field.
* @return a string containing the data
*/
virtual const UnicodeString getString(const char* key, UErrorCode &status) const = 0;
virtual UnicodeString getString(const char* key, UErrorCode &status) const = 0;

/** get the string from the DataMap. Addressed by name
* parses a bundle string into an integer
Expand Down Expand Up @@ -121,7 +121,7 @@ class T_CTEST_EXPORT_API RBDataMap : public DataMap{

virtual const ResourceBundle *getItem(const char* key, UErrorCode &status) const;

virtual const UnicodeString getString(const char* key, UErrorCode &status) const override;
virtual UnicodeString getString(const char* key, UErrorCode &status) const override;
virtual int32_t getInt28(const char* key, UErrorCode &status) const override;
virtual uint32_t getUInt28(const char* key, UErrorCode &status) const override;
virtual const int32_t *getIntVector(int32_t &length, const char *key, UErrorCode &status) const override;
Expand Down

0 comments on commit fb64693

Please sign in to comment.