0

I've got a very simple piece of code
class instance assignement:

class ABC {
public:    
    void set(const ABC* obj) {
        *this = *obj;
    }

    ABC& operator=(const ABC* obj);
};  

ABC& ABC::operator = (const ABC *obj){
    if(obj!= this) set(obj);
    return *this;
}

This is a classic book example,
but CLing-Tidy highlight 'ABC&' in method realization line

ABC& ABC::operator = (const ABC *obj){
     ...

says:

Clang-Tidy: Operator=() should return 'ABC&'

Could you suggest what is wrong with this code?
It works, but Clang-Tidy doesn't like it.

Thanks in advance guys,
Have a nice code.

sinoptic
  • 303
  • 3
  • 12
  • 2
    Your example isn't complete. [This](https://godbolt.org/z/jEv88Mh7W) works. Please post [minimal-reproducible-example](https://stackoverflow.com/help/minimal-reproducible-example). – VainMan Sep 15 '21 at 06:47
  • Is that all it says? Doesn't it also inform you about what rule your operator violates? Please paste the full `clang-tidy` output. What version of `clang-tidy` are you using? – Ted Lyngmo Sep 15 '21 at 07:29
  • Unrelated: Why do you have such a unconventional constructor instead of `ABC& ABC::operator=(const ABC& rhs)`? – Ted Lyngmo Sep 15 '21 at 07:30
  • @TedLyngmo clang-tidy = LLVM version 10.0.0. That is all it says, no rule violation messages. I use CLang-Tidy with CLion. (?)"Why do you have such a unconventional constructor ..." >> I use both of them, that is a matter convenience, here is just an example of real big class. In every method "operator =(...)" I've got the same CLang-Tidy warning – sinoptic Sep 15 '21 at 08:02
  • Then either CLion is hiding that information _or_ it's not `clang-tidy` that provides the information. `clang-tidy` itself prints the violated rule(s) after the message in the format `[` ... _comma separated violated rules_ ... `]` – Ted Lyngmo Sep 15 '21 at 08:05
  • 1
    Regarding the unconventional constructor: "_I use both of them, that is a matter convenience, here is just an example of real big class_" - I've got a feeling that you make it unnecessarily complicated by providing such constructors. You also don't check for `nullptr` in `this = *obj;` – Ted Lyngmo Sep 15 '21 at 08:09
  • typo^ `*this = *obj;` – Ted Lyngmo Sep 15 '21 at 08:40

0 Answers0