Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 3a41f22

Browse files
committed
Exploring emitting other sorts of spans
1 parent ab9fa25 commit 3a41f22

File tree

1 file changed

+32
-10
lines changed

1 file changed

+32
-10
lines changed

clippy_lints/src/trailing_zero_sized_array_without_repr.rs

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
use clippy_utils::diagnostics::span_lint_and_help;
1+
use clippy_utils::{diagnostics::{span_lint_and_help, span_lint_and_then, span_lint_and_sugg}, source::{indent_of, snippet}};
22
use rustc_ast::Attribute;
3+
use rustc_errors::Applicability;
34
use rustc_hir::{Item, ItemKind};
45
use rustc_lint::{LateContext, LateLintPass};
56
use rustc_middle::dep_graph::DepContext;
@@ -50,14 +51,34 @@ impl<'tcx> LateLintPass<'tcx> for TrailingZeroSizedArrayWithoutRepr {
5051
};
5152

5253
if !has_repr_attr(cx, attrs) {
53-
span_lint_and_help(
54-
cx,
55-
TRAILING_ZERO_SIZED_ARRAY_WITHOUT_REPR,
56-
lint_span,
57-
"trailing zero-sized array in a struct which is not marked with a `repr` attribute",
58-
None,
59-
"consider annotating the struct definition with `#[repr(C)]` or another `repr` attribute",
60-
);
54+
let suggestion_span = item.span.shrink_to_lo();
55+
let indent = " ".repeat(indent_of(cx, item.span).unwrap_or(0));
56+
57+
span_lint_and_sugg(cx, TRAILING_ZERO_SIZED_ARRAY_WITHOUT_REPR, item.span, "trailing zero-sized array in a struct which is not marked with a `repr` attribute", "consider adding `#[repr(C)]` or another `repr` attribute", format!("#[repr(C)]\n{}", snippet(cx, item.span.shrink_to_lo().to(item.ident.span), "..")), Applicability::MaybeIncorrect);
58+
59+
// span_lint_and_then(
60+
// cx,
61+
// TRAILING_ZERO_SIZED_ARRAY_WITHOUT_REPR,
62+
// item.span,
63+
// "trailing zero-sized array in a struct which is not marked with a `repr` attribute",
64+
// |diag| {
65+
// let sugg = format!("#[repr(C)]\n{}", indent);
66+
// let sugg2 = format!("#[repr(C)]\n{}", item.ident.span);
67+
// diag.span_suggestion(item.span,
68+
// "consider adding `#[repr(C)]` or another `repr` attribute",
69+
// sugg2,
70+
// Applicability::MaybeIncorrect);
71+
// }
72+
// );
73+
74+
// span_lint_and_help(
75+
// cx,
76+
// TRAILING_ZERO_SIZED_ARRAY_WITHOUT_REPR,
77+
// lint_span,
78+
// "trailing zero-sized array in a struct which is not marked with a `repr` attribute",
79+
// None,
80+
// "consider annotating the struct definition with `#[repr(C)]` or another `repr` attribute",
81+
// );
6182
}
6283
}
6384
}
@@ -91,7 +112,8 @@ fn has_repr_attr(cx: &LateContext<'tcx>, attrs: &[Attribute]) -> bool {
91112
// NOTE: there's at least four other ways to do this but I liked this one the best. (All five agreed
92113
// on all testcases (when i wrote this comment. I added a few since then).) Happy to use another;
93114
// they're in the commit history if you want to look (or I can go find them).
115+
let sess = cx.tcx.sess(); // are captured values in closures evaluated once or every time?
94116
attrs
95117
.iter()
96-
.any(|attr| !rustc_attr::find_repr_attrs(cx.tcx.sess(), attr).is_empty())
118+
.any(|attr| !rustc_attr::find_repr_attrs(sess, attr).is_empty())
97119
}

0 commit comments

Comments
 (0)