共用方式為


lnt-naming-convention

請確定符號的命名慣例符合程式碼撰寫樣式,如專案 .editorconfig 檔案中所指定。

若要啟用此功能,請在與專案檔相同的目錄中新增 .editorconfig 檔案。 .editorconfig會指定專案中符號的命名慣例。 例如,Unreal Engine 專案的命名慣例是在 GitHub 上的 中 .editorconfig 指定。

一旦您在專案中有 .editorconfig 檔案,請在 C/C++ 程式碼樣式選項中開啟 lnt-naming-convention [命名慣例] 設定的核 取。 如需如何變更此設定的資訊,請參閱 設定 linter

範例

假設您有一個 .editorconfig 檔案,其中包含:

cpp_naming_style.boolean_style.capitalization = pascal_case
cpp_naming_style.boolean_style.required_prefix = b

linter 會標幟下列程式碼,因為它前面未加上 'b',而且因為它不是 Pascal 大小寫,如檔案中所 .editorconfig 指定:

void example()
{
    bool myFlag = true; // flagged because it doesn't follow the naming convention specified in the .editorconfig
}

如何修正問題

變更命名以符合 中指定的 .editorconfig 樣式:

void example()
{ 
    bool bMyFlag = true; // fixed to follow the code style specified in the .editorconfig
} 

編輯器可以為您進行變更。 將游標放在標幟的符號上。 選擇 [ 顯示潛在的修正 程式],然後選擇 [ 套用命名慣例 ]:

Screenshot of the IDE suggesting applying naming convention.

程式碼編輯器會顯示 bool myFlag = true。 在該行程式碼上具有游標,[顯示可能的修正程式] 隨即出現並已選擇。 現在會顯示 [套用命名慣例] ,而且會顯示 bool my Flag = true,並以綠色顯示建議的變更 bool b My Flag。 您現在可以選擇 [套用命名慣例] ,將標幟的程式碼變更為 bool b My Flag = true。

備註

在 Visual Studio 2022 17.7 中引進, lnt-naming-convention linter 檢查可確保命名慣例與檔案中指定的 .editorconfig 慣例一致。 您可以將此檢查套用至具有 .editorconfig 檔案的任何專案。 您也可以自訂檔案 .editorconfig ,以符合專案的編碼樣式。

另請參閱

使用 EditorConfig 建立可攜式自訂編輯器設定
適用于 C++ 的 IntelliSense 程式碼 linter 概觀