Skip to content

clang-format option AlignTrailingComments: Leave misaligns comment-only lines #92530

@MatejVitek

Description

@MatejVitek

I'm using clang-format in VS Code (via Microsoft's C/C++ extension) with the following setup:

"C_Cpp.clang_format_style": "{BasedOnStyle: LLVM, UseTab: ForIndentation, IndentWidth: 4, TabWidth: 4, ColumnLimit: 0, PointerAlignment: Left, BreakBeforeBraces: Attach, SortIncludes: CaseInsensitive, AllowShortIfStatementsOnASingleLine: Never, AlignTrailingComments: Leave, SpacesBeforeTrailingComments: 2, SpaceBeforeCaseColon: false, InsertNewlineAtEOF: true}",

However, this setup is indenting my comment lines incorrectly. My expected result would look like:

int main(int argc, char* argv[]) {
	// create 10 threads
	pthread_t threads[10];
	for (int i = 0; i < 10; i++)
		pthread_create(&threads[i], NULL, thread, NULL);

	// wait for a bit then stop the threads
	sleep(3);
	for (int i = 0; i < 10; i++) {
		// pthread_yield();  // deprecated
		sched_yield();
		pthread_cancel(threads[i]);  // cancel thread
	}
}

However, the result of formatting with the above setup is the following:

int main(int argc, char* argv[]) {
 // create 10 threads
	pthread_t threads[10];
	for (int i = 0; i < 10; i++)
		pthread_create(&threads[i], NULL, thread, NULL);

 // wait for a bit then stop the threads
	sleep(3);
	for (int i = 0; i < 10; i++) {
  // pthread_yield();  // deprecated
		sched_yield();
		pthread_cancel(threads[i]);  // cancel thread
	}
}

This seems to be caused by the setting AlignTrailingComments: Leave. Setting this option to either Never or Always formats comment-only lines correctly (but obviously correspondingly changes the behaviour for actual trailing comments).

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions