Skip to content

Fix single_char_pattern crash (#3204) #3213

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
1 commit merged into from Sep 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions clippy_lints/src/methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ use crate::utils::{get_arg_name, get_trait_def_id, implements_trait, in_macro, i
span_lint, span_lint_and_sugg, span_lint_and_then, span_note_and_lint, walk_ptrs_ty, walk_ptrs_ty_depth, SpanlessEq};
use crate::utils::paths;
use crate::utils::sugg;
use crate::consts::{constant, Constant};
use crate::rustc_errors::Applicability;

#[derive(Clone)]
Expand Down Expand Up @@ -1914,8 +1913,11 @@ fn lint_chars_last_cmp_with_unwrap<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, info: &

/// lint for length-1 `str`s for methods in `PATTERN_METHODS`
fn lint_single_char_pattern<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, _expr: &'tcx hir::Expr, arg: &'tcx hir::Expr) {
if let Some((Constant::Str(r), _)) = constant(cx, cx.tables, arg) {
if r.len() == 1 {
if_chain! {
if let hir::ExprKind::Lit(lit) = &arg.node;
if let ast::LitKind::Str(r, _) = lit.node;
if r.as_str().len() == 1;
then {
let snip = snippet(cx, arg.span, "..");
let hint = format!("'{}'", &snip[1..snip.len() - 1]);
span_lint_and_sugg(
Expand Down
4 changes: 4 additions & 0 deletions tests/ui/single_char_pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,8 @@ fn main() {

x.replace(";", ",").split(","); // issue #2978
x.starts_with("\x03"); // issue #2996

// Issue #3204
const S: &str = "#";
x.find(S);
}