簡體   English   中英

用於捕獲未分配的r值的編譯器選項

[英]Compiler option to catch unassigned r-values

相當尷尬的是,我寫了類似下面的東西(消毒過):

vector_item next()
{
    if (this->it == this->myVector.end()){
        this->it == this->myVector.begin();
    }
    return *(this->it++);
}

明顯的錯誤很明顯。 然而,它確實需要一段時間來追蹤。

沒有為此生成編譯器警告,但是應該有嗎? 在這里創建了一個未使用的r值(並且不是說,來自為其副作用調用的函數的未使用的返回值),這在我看來是代碼存在問題的指示器。

g++ -Wall -Wextra編譯。 (GCC 4.8.3)

我知道-Wunused-result但這不適用於此。

沒有為此生成編譯器警告,但是應該有嗎?

我想不出我在標准中讀到的任何需要警告的內容。

然而,clang開發團隊似乎認為它保證一個:

18 : <source>:18:18: warning: equality comparison result unused [-Wunused-comparison]
        this->it == this->myVector.begin();
        ~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
18 : <source>:18:18: note: use '=' to turn this equality comparison into an assignment
        this->it == this->myVector.begin();
                 ^~
                 =
1 warning generated.

我的第一個想法是,它是gcc中的QoI問題。 可能值得提出它作為一個問題。

我很幸運,因為我們的軟件是針對mac(clang),linux(gcc)和windows(msvc)編譯的,因此可以及早發現標准侵權和邊緣案例。

可能會想到在另一個編譯器上重新編譯你的代碼然后再進行一次bug搜索 - 這對我很有幫助。

沒有為此生成編譯器警告,但是應該有嗎?

標准中沒有任何聲明可以使GCC為該案例生成警告。

你可以通過將它標記為WARN_UNUSED來擴展begin() ,首先你要定義:

#define WARN_UNUSED __attribute__((warn_unused_result))

正如這里所描述的那樣,但這當然不是你想要的,但它是一些東西。 我找不到GCC的任何選項來為您的案例生成警告。

雖然這是一個已知的GCC錯誤 ,但尚未實現您正在尋找的功能,至少要到2017-07-21。


但是,clang 6.0.0會發出警告,(即使不使用WallWextra標志):

prog.cc:11:18: warning: equality comparison result unused [-Wunused-comparison]
        this->it == this->myVector.begin();
        ~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
prog.cc:11:18: note: use '=' to turn this equality comparison into an assignment
        this->it == this->myVector.begin();
                 ^~
                 =
1 warning generated.

此外,zapcc 1.0.1也發出警告(即使沒有警告標志):

/home/jail/prog.cc:11:18: warning: equality comparison result unused [-Wunused-comparison]
        this->it == this->myVector.begin();
        ~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
/home/jail/prog.cc:11:18: note: use '=' to turn this equality comparison into an assignment
        this->it == this->myVector.begin();
                 ^~
                 =
1 warning generated.

如果你願意,可以在Wandbox中自己查看。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM