crow-translate / QOnlineTranslator

A library for Qt5 that provides free usage of Google, Yandex and Bing translate API.
GNU General Public License v3.0
79 stars 12 forks source link

Improperly terminated line #15

Closed Light-Wizzard closed 2 years ago

Light-Wizzard commented 2 years ago

Not sure why this compiles since it creates two lines of code, when it should be one:

    const QByteArray postData = "&text=" + QUrl::toPercentEncoding(sender()->property(s_textProperty).toString());
    + "&from=" + languageApiCode(Bing, m_sourceLang).toUtf8()
            + "&to=" + languageApiCode(Bing, m_translationLang).toUtf8();

// Removed terminator from end of line
    const QByteArray postData = "&text=" + QUrl::toPercentEncoding(sender()->property(s_textProperty).toString())
    + "&from=" + languageApiCode(Bing, m_sourceLang).toUtf8()
            + "&to=" + languageApiCode(Bing, m_translationLang).toUtf8();

https://github.com/crow-translate/QOnlineTranslator/blob/master/src/qonlinetranslator.cpp#L1442

Thanks, Flesh

Shatur commented 2 years ago

Wow, yes, it was a typo, thanks. Strange, I didn't get any warnings from the compiler or from the static analyzer.

Light-Wizzard commented 2 years ago

I have no idea why it did not give an error, crazy I know, I was going line by line through the code and saw it, at first I was not sure, so I deleted it, ran the app, and found it gave a different result, it was ignoring the + line completely, I found that it optimized it out, and that is when I knew it was a typo the compiler should have caught, it knew it was dead code, but gave no warning at compile-time, a bug for sure, maybe it has been reported, did not look, but code checker gives a warning about it, but you have to enable a lot more checks to see that warning about unused code.

Flesh

Shatur commented 2 years ago

I have no warnings in Clang Tidy, the most popular checked. Also two compilers (MSVC and GCC) doesn't say a word.

Light-Wizzard commented 2 years ago

I use Qt Creator, stock install for Arch Linux Manjaro, it uses /usr/bin/cppcheck, but you need to enable more test, I do it under Tools -> Options -> Analyzer -> and check all of them for Cppcheck Tab, but I found this the old fashion way, I was looking at the code at GitHub and it just popped out as odd, at first I had no idea why it compiles, but plus + is a strange concept with the compiler, it just appends to the last command, and just ignores anything it cannot append to, strange behavior, but it is not a bug according to that feature, it does not care what you add something to, empty braces prove that, so it is just a strange thing you need to look out for, according to the standard, you can do this, and there might be a case for it, but I have no idea what that might be, but you can compile a program with a call to + as long as the syntac is correct, in this case it was, it will make a string, and put the content into that string, but it has no method to call it, so it ends up being an unused variable or dead code, but if you made a call inside that string call, it will get executed, thus it becomes part of the code, but still marked as unused, I doubled checked this and it is right, although very wrong at the same time.

I started programming in C back in the late 60s and early 70s, and remembered when C++ added the concept of Classes to the + operator, this gives you the ability to do stuff like {} + "This"; , but if {} is empty, it gets dropped, and why + "This"; works, it uses a macro to achieve this level of insanity, but that C++ for you.

Flesh