Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
jens1o committed Mar 10, 2019
1 parent 9308df7 commit b76f939
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 50 deletions.
73 changes: 36 additions & 37 deletions clippy_lints/src/strings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,44 +170,43 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for StringLitAsBytes {
if path.ident.name == "as_bytes" {
if let ExprKind::Lit(ref lit) = args[0].node {
if let LitKind::Str(ref lit_content, style) = lit.node {
let callsite = snippet(cx, args[0].span.source_callsite(), r#""foo""#);
let expanded = if let StrStyle::Raw(n) = style {
let term = (0..n).map(|_| '#').collect::<String>();
format!("r{0}\"{1}\"{0}", term, lit_content.as_str())
} else {
format!("\"{}\"", lit_content.as_str())
};
let mut applicability = Applicability::MachineApplicable;
if callsite.starts_with("include_str!") {
span_lint_and_sugg(
cx,
STRING_LIT_AS_BYTES,
e.span,
"calling `as_bytes()` on `include_str!(..)`",
"consider using `include_bytes!(..)` instead",
snippet_with_applicability(cx, args[0].span, r#""foo""#, &mut applicability).replacen(
"include_str",
"include_bytes",
1,
),
applicability,
);
} else if callsite == expanded
&& lit_content.as_str().chars().all(|c| c.is_ascii())
&& !in_macro(args[0].span)
{
span_lint_and_sugg(
cx,
STRING_LIT_AS_BYTES,
e.span,
"calling `as_bytes()` on a string literal",
"consider using a byte string literal instead",
format!(
"b{}",
if lit_content.as_str().len() <= 32 {
let callsite = snippet(cx, args[0].span.source_callsite(), r#""foo""#);
let expanded = if let StrStyle::Raw(n) = style {
let term = (0..n).map(|_| '#').collect::<String>();
format!("r{0}\"{1}\"{0}", term, lit_content.as_str())
} else {
format!("\"{}\"", lit_content.as_str())
};
let mut applicability = Applicability::MachineApplicable;
if callsite.starts_with("include_str!") {
span_lint_and_sugg(
cx,
STRING_LIT_AS_BYTES,
e.span,
"calling `as_bytes()` on `include_str!(..)`",
"consider using `include_bytes!(..)` instead",
snippet_with_applicability(cx, args[0].span, r#""foo""#, &mut applicability)
),
applicability,
);
.replacen("include_str", "include_bytes", 1),
applicability,
);
} else if callsite == expanded
&& lit_content.as_str().chars().all(|c| c.is_ascii())
&& !in_macro(args[0].span)
{
span_lint_and_sugg(
cx,
STRING_LIT_AS_BYTES,
e.span,
"calling `as_bytes()` on a string literal",
"consider using a byte string literal instead",
format!(
"b{}",
snippet_with_applicability(cx, args[0].span, r#""foo""#, &mut applicability)
),
applicability,
);
}
}
}
}
Expand Down
14 changes: 1 addition & 13 deletions tests/ui/strings.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,5 @@ LL | let bs = "hello there".as_bytes();
|
= note: `-D clippy::string-lit-as-bytes` implied by `-D warnings`

error: calling `as_bytes()` on a string literal
--> $DIR/strings.rs:50:14
|
LL | let bs = r###"raw string with three ### in it and some " ""###.as_bytes();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using a byte string literal instead: `br###"raw string with three ### in it and some " ""###`

error: calling `as_bytes()` on `include_str!(..)`
--> $DIR/strings.rs:57:22
|
LL | let includestr = include_str!("entry.rs").as_bytes();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `include_bytes!(..)` instead: `include_bytes!("entry.rs")`

error: aborting due to 11 previous errors
error: aborting due to 9 previous errors

0 comments on commit b76f939

Please sign in to comment.